繁体   English   中英

多线程程序线程连接问题

[英]Multithreaded program thread join issues

我目前正在编写一个多线程程序,根据某些情况,有时可能会创建一个线程。 如果创建了这个线程,它需要独立于所有其他线程运行,我不能阻止任何其他线程等待它加入。 生成的线程运行的时间长度各不相同; 有时它可能需要几个小时。

我已经尝试生成线程并在类的析构函数中添加一个正常工作的连接,但是如果生成的线程中的代码在析构函数被调用之前很长时间(这将是99%的时间)我会像杀死自己的线程释放所有资源等。

我考虑使用detach,但你不能重新加入一个分离的线程,并且在这个线程完成之前调用析构函数然后生成的线程将无法完成并且可能产生灾难性的后果。

是否有任何可能的解决方案可以确保线程在类被破坏之前完成,并且一旦线程完成其工作就允许它加入?

我正在使用boost / c ++ 11进行线程化。 任何帮助都将非常感激。

谢谢

线程可以自行分离,释放其资源。 如果析构函数看到线程可以连接,即仍然在运行,那么让它加入。 如果线程到达终点,则自行分离。 可能的竞争条件:is_joinable()在析构函数中返回true - 线程分离自身 - 析构函数连接并失败。 所以使用一个互斥锁来保护线程的死亡:

struct ThreadContainer
{
   std::mutex threadEndMutex;
   std::thread theThread;

   ThreadContainer()
     : theThread([=]()
       {
         /* do stuff */

         // if the mutex is locked, the destructor is just
         // about to join, so we let him.
         if (threadEndMutex.try_lock())
           theThread.detach();
       })
   {}

   ~ThreadContainer()
   {
     // if the mutex is locked, the thread is just about 
     // to detach itself, so no need to join.
     // if we got the mutex but the thread is not joinable, 
     // it has detached itself already.
     if (threadEndMutex.try_lock() && theThread.is_joinable())
       theThread.join();
   }
};

PS:你可能甚至不需要调用is_joinable,因为如果线程自己分离,它永远不会解锁互斥锁并且try_lock失败。

PPS:您可以使用std :: atomic_flag代替互斥锁:

struct ThreadContainer
{
   std::atmoic_flag threadEnded;
   std::thread theThread;

   ThreadContainer()
     : threadEnded(ATOMIC_FLAG_INIT)
     , theThread([=]()
       {
         /* do stuff */

         if (!threadEnded.test_and_set())
           theThread.detach();
       })
   {}

   ~ThreadContainer()
   {
     if (!threadEnded.test_and_set())
       theThread.join();
   }
};

您可以在“独立”线程算法中定义暂停/步骤,并在每个步骤中查看一个全局变量,该变量可帮助您决定取消计算并自动销毁,或继续计算您的线程。

如果全局变量不够,即如果需要更精确的粒度,则应为线程函数定义一个仿函数对象,该仿函数具有方法kill()。 在将它们作为线程启动后,您将保留对仿函数的引用。 当你调用MyThreadFunctor :: kill()时,它会设置一个布尔字段,并在functor线程函数本身的计算的每个步骤中检查此字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM