boost::thread interrupt() 仅在第一次执行中断点时引发异常?
Posted
技术标签:
【中文标题】boost::thread interrupt() 仅在第一次执行中断点时引发异常?【英文标题】:boost::thread interrupt() throws an exception only the first time that an interruption point is executed? 【发布时间】:2014-10-03 07:21:51 【问题描述】:boost::thread interrupt() 仅在第一次执行中断点时抛出异常?我的情况如下。
我创建了一个boost::thread
,它执行functionA.
functionA
调用functionB
,当在functionB
上时,我们调用函数threadInterrupt.
functionB
抛出一个boost::thread_interrupted
异常并返回functionA.
我的问题是在执行另一个中断点时是否会在函数A中抛出一个新的boost::thread_interrupted
异常。
void MyClass::function(int arg1, int arg2) try
boost::thread* t = new boost::thread(boost::bind(&MyClass::functionA, this, var1, var2));
catch (...)
cout << "function throw an exception" << endl;
void MyClass::functionA(int arg1, int arg2 ) try
//some code here
try
int retVal = MyClass::functionB(var);
boost::this_thread::interruption_point();
//some code here
catch(boost::thread_interrupted&)
cout << "thread interrupted in functionA" << endl;
catch (...)
cout << "functionA throw an exception" << endl;
int MyClass::functionB(int arg1) try
//some code here
try
boost::this_thread::interruption_point();
//some code here
catch(boost::thread_interrupted&)
cout << "thread interrupted in functionB" << endl;
return 0;
return 1;
catch (...)
cout << "functionB throw an exception" << endl;
return 1;
void MyClass::threadInterrupt()
if (thr!=NULL)
thr->interrupt();
thr->join();
delete thr;
thr=NULL;
【问题讨论】:
【参考方案1】:你试过了吗?看到它Live On Coliru
#include <boost/thread.hpp>
#include <iostream>
using namespace boost;
int main()
thread t([]
int i = 0;
while (true) try
if (i >= 10) return;
while (i < 10)
this_thread::sleep_for(chrono::milliseconds(200));
std::cout << "processed " << i++ << "\n";
catch (...) std::cout << "Interrupted at i = " << i << "\n";
);
this_thread::sleep_for(chrono::milliseconds(800));
t.interrupt();
t.join();
std::cout << "Interrupt requested? : " << std::boolalpha << t.interruption_requested() << "\n";
输出:
processed 0
processed 1
processed 2
Interrupted at i = 3
processed 3
processed 4
processed 5
processed 6
processed 7
processed 8
processed 9
Interrupt requested? : false
如您所见,中断请求的行为类似于一种自动复位标志。
【讨论】:
感谢您的回复。我修改了我的代码并在函数Bstd::cout << "Interrupt requested in functionB? : " << std::bool alpha << boost::this_thread::interruption_requested() << "\n" << endl;
和函数A std::cout << "Interrupt requested in functionB? : " << std::bool alpha << boost::this_thread::interruption_requested() << "\n" << endl;
中添加,我得到的输出是Interrupt requested in functionB? :true Interrupt requested in functionA? :false
@KoKa 你去 :) 回答你自己的问题:一旦抛出 thread_interrupted
异常,“标志”就会被取消。
我的情况是如果functionB被中断,functionA也应该被中断,我不希望functionA的代码被执行。有什么办法可以让functionA的interrupt_point也被执行?
@KoKa 您可以中断正在运行的线程(在某些地方引用它)。您可以自己抛出interrupted_exception
(我建议使用自定义类型以避免混淆);或者为什么不用返回值就知道任务需要取消了?
我正在中断正在运行的线程,或者至少我认为我正在这样做!当我创建新线程 (void MyClass::function) 时,我保留对它的引用 (boost::thread* t),我将其中断并加入函数 void MyClass::threadInterrupt()。难道我做错了什么?您能否解释一下如何使用自己抛出的 interrupted_exception 来中断这两个函数的代码执行?以上是关于boost::thread interrupt() 仅在第一次执行中断点时引发异常?的主要内容,如果未能解决你的问题,请参考以下文章
Boost.Thread 链接 - boost_thread 与 boost_thread-mt
boost::this_thread::interruption_point() 不会抛出 boost::thread_interrupted& 异常