C ++ Boost线程ID访问

时间:2014-12-19 01:42:06

标签: c++ multithreading boost

我对线程ID访问有疑问。在系统级别上,我有多个线程同时并行运行。在任何给定的时间,我希望父线程/主线程能够通过线程ID中断特定的生成线程。阅读Boost的线程管理文档,我发现我可以这样做:

//global 
boost::thread::id threadID = boost::thread::id(); //at this point, it's defined as a not-any-thread

void thread_to_interrupt()
{
    threadID = boost::this_thread::get_id(); //grabs current thread ID and stores it in global
}

void main()
{
    //some code here will set condition_for_new_thread to TRUE
    if(condition_for_new_thread == 1)
    {
        boost::thread newThread(&thread_to_interrupt);
    }
    ... //other process stuff
    //NOTE: at this point I want to interrupt thread_to_interrupt() based on thread ID
    if(condition_to_interrupt_thread == 1)
    {
        newThread.interrupt(); //won't compile here because newThread was created inside an if-condition
        //Maybe try to interrupt based on threadID somehow? Not sure how to implement it
    }
}

基本上,我如何根据线程ID进行中断?我可以使用通用功能吗?或者,我可以初始化一个新线程但不将其指向任何子例程,以便newThread.interrupt()将编译。

任何帮助将不胜感激,谢谢!

0 个答案:

没有答案