1
我使用此代码进行了测试。两个线程thread_1
每200毫秒打印一个字符串,thread_2
在3秒内终止thread_1
。为什么升压this_thread :: interrupt可以在没有try-catch的情况下工作?
int main() {
boost::thread t1([&]() {
while(1) {
boost::this_thread::interruption_point();
cout << "t1 running" << endl;
Sleep(200);
}
cout << "it never goes here" << endl;
});
boost::thread t2([&]() {
Sleep(3000); // wait 3 seconds to terminate thread 1
cout << "interrupt t1" << endl;
t1.interrupt(); // thread 1 should raise a thread_interrupted exception ?
});
t1.join();
t2.join();
Sleep(5000); // sleep 5 seconds waiting for the crash
}
我希望代码崩溃,但事实并非如此。然后我猜boost::thread
有一个try-catch,我在所有的“thread.hpp”和“thread_data.hpp”中搜索关键字catch
,但什么也没找到。
它是如何工作的?谢谢。