C++ 告诉std::线程在满足条件时终止/停止自身

C++ 告诉std::线程在满足条件时终止/停止自身,c++,multithreading,c++11,stdthread,C++,Multithreading,C++11,Stdthread,假设我有一个worker线程tWorker,它在构建Boss时初始化,并告诉它执行work(),直到breread为真。std::mutex,mtx,锁定一些数据(vFiles),以便tWorker在处理数据时拥有它 一旦breret变成true,我如何让tWorker自杀?当线程停止执行时,mutex将如何销毁 我读过std::thread对象不能以任何方式中断。让线程什么都不做(或调用std::this_thread::yield())是否会产生与终止线程相同的效果 class Boss {

假设我有一个worker线程
tWorker
,它在构建
Boss
时初始化,并告诉它执行
work()
,直到
breread
为真。
std::mutex
mtx
,锁定一些数据(
vFiles
),以便
tWorker
在处理数据时拥有它

一旦
breret
变成
true
,我如何让
tWorker
自杀?当线程停止执行时,
mutex
将如何销毁

我读过
std::thread
对象不能以任何方式中断。让线程什么都不做(或调用
std::this_thread::yield()
)是否会产生与终止线程相同的效果

class Boss {
private:
    std::thread tWorker;
    std::mutex mtx;
    bool bRetired;
    std::vector< std::string > vFiles;

    void work() {
        while ( bRetired == false ) {
            // Do your job!
            mtx.lock();
            // ... Do something about vFiles ...
            mtx.unlock();
        }

        // tWorker has retired, commit suicide
        // ** How? **

        // Does this suffice if I want to "kill" the thread?
        std::this_thread::yield(); 
    }

public:
    Boss() {
        bRetired = false;
        tWorker = std::thread( &Boss::work, this );

        // Have worker do its job independently
        // **Bonus Question** : Should this be tWorker.join() or tWorker.detach()?
        tWorker.detach();
    }

    retire() {
        bRetired = true;
    }
}
类Boss{
私人:
std::threadtworker;
std::互斥mtx;
布尔·布雷特;
std::vectorvFiles;
无效工作(){
while(bretreat==false){
//做好你的工作!
mtx.lock();
//…对vFiles做点什么。。。
mtx.unlock();
}
//tWorker退休了,自杀了
//**如何**
//如果我想“杀死”线程,这就足够了吗?
std::this_thread::yield();
}
公众:
老板(){
布雷特=假;
tWorker=std::thread(&Boss::work,this);
//让工人独立完成工作
//**额外问题**:这应该是tWorker.join()还是tWorker.detach()?
tWorker.detach();
}
退休{
布雷特=真;
}
}
注释

  • 工作线程一旦失效,就无法再次启动
  • 工作线程在后台工作,不会中断主线程的执行
    • 对的调用是不需要的,并且不会终止调用线程:

      向实现提供重新安排线程执行的提示,以允许其他线程运行

      只需退出函数即可退出线程

      请注意,由于两个线程可以访问同一个内存位置,而其中一个线程正在修改该位置,因此使用
      bretreed
      是不正确的:这是未定义的行为。另外,执行
      run()
      :用于原子性和可见性的线程将看不到在不同线程的函数
      retire()
      中所做的更改

      如果在构造函数中使用了,则构造函数将在线程退出之前不会返回,因为不可能调用
      retire()
      ,因为对象将不可用(因为构造函数不会返回)。如果需要与线程的退出同步,则在
      retire()
      函数中不要
      detach()
      ,而是
      join()

      void retire() {
          bRetired = true;
          tWorker.join();
      }
      
      使用RAII获取
      互斥锁
      es(例如),以确保始终释放该互斥锁。
      mutex
      将在其超出范围时被销毁,在这种情况下,当其包含的类被销毁时

      一旦布雷特变成现实,我该如何让tWorker“自杀”

      让控制流退出线程函数。那
      std::this_thread::yield()
      调用是不必要的

      当线程停止执行时,互斥锁将如何销毁

      该互斥锁是
      Boss
      类的成员。当对象被销毁时,它会在
      Boss
      的析构函数中被销毁

      我读过std::thread对象不能以任何方式中断

      C++API不提供终止任意线程的方法。必须有一种方法告诉线程终止,然后等待它终止,就像您打算做的那样

      让线程什么都不做(或调用std::this_thread::yield())是否会提供与终止线程相同的效果

      class Boss {
      private:
          std::thread tWorker;
          std::mutex mtx;
          bool bRetired;
          std::vector< std::string > vFiles;
      
          void work() {
              while ( bRetired == false ) {
                  // Do your job!
                  mtx.lock();
                  // ... Do something about vFiles ...
                  mtx.unlock();
              }
      
              // tWorker has retired, commit suicide
              // ** How? **
      
              // Does this suffice if I want to "kill" the thread?
              std::this_thread::yield(); 
          }
      
      public:
          Boss() {
              bRetired = false;
              tWorker = std::thread( &Boss::work, this );
      
              // Have worker do its job independently
              // **Bonus Question** : Should this be tWorker.join() or tWorker.detach()?
              tWorker.detach();
          }
      
          retire() {
              bRetired = true;
          }
      }
      
      没有


      不过,
      bretreed
      变量上存在竞争条件。它要么需要是
      std::atomic
      ,要么只有当互斥锁被锁定时才应该读取和修改它。

      那么,在
      循环期间中断
      将停止线程?假设我添加了另一个
      bool
      ,它是
      bSleep
      ,以使线程休眠或在我告诉它之前什么也不做(不是在特定的时间内),我应该从函数返回还是使用
      yield()
      ?@LanceGray,请参阅实现等待直到我告诉你做某事的机制。如果我理解正确,
      std::condition\u变量
      类似于
      pthread\u cond\u wait
      。是吗?是的,根据您的操作系统,实现可能会在内部使用类似的东西。