从另一个取消提升线程

0 c++ multithreading boost

有没有办法从另一个取消boost :: thread,如下所示?:

boost::thread* thread1(0);
boost::thread* thread2(0);

thread2 = new boost::thread([&](){
   //some expensive computation that can't be modified
  if(thread1)
    thread1->interrupt();  
});

thread1 = new boost::thread([&]() {
  //some other expensive computation that can't be modified
  if(thread2)
    thread2->interrupt();  
});

thread1->join();
thread2->join();

delete thread1;
delete thread2;
Run Code Online (Sandbox Code Playgroud)

现在,两个昂贵的计算都完成而不会被打断.我认为连接将被视为中断点,主线程将在完成两个昂贵的计算之一后继续.

Ami*_*ory 5

通常,一个线程没有可移植的方式来终止另一个线程,而没有来自被终止的线程的协作.似乎偶尔会出现这个问题(看到这里这里 - 虽然你的问题并不完全相同).

禁止线程被中断的合作(必须在通知时执行seppuku),如果您希望主线程在第一个线程终止后继续,您可以使condition每个子线程在其结束时触发.

此时,你可以让另一个线程继续运行(可能是detach它),或者只是终止一切.