C++ 为什么boost this_thread::interrupt可以在没有try-catch的情况下工作?
我用这个代码进行了测试。两个线程,线程1每200毫秒打印一个字符串,线程2在3秒内终止线程1C++ 为什么boost this_thread::interrupt可以在没有try-catch的情况下工作?,c++,multithreading,boost,C++,Multithreading,Boost,我用这个代码进行了测试。两个线程,线程1每200毫秒打印一个字符串,线程2在3秒内终止线程1 int main() { boost::thread t1([&]() { while(1) { boost::this_thread::interruption_point(); cout << "t1 running" << endl; Sleep(200);
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,但没有找到任何内容
它是如何工作的?谢谢。库实现包含源文件,而不是头文件,其中包含处理错误的代理函数 正如您在phtreads变体中所看到的,它还执行一些其他内务管理:
库实现包含源文件,而不是头文件,其中包含处理错误的代理函数 正如您在phtreads变体中所看到的,它还执行一些其他内务管理:
namespace
{
extern "C"
{
static void* thread_proxy(void* param)
{
boost::detail::thread_data_ptr thread_info = static_cast<boost::detail::thread_data_base*>(param)->self;
thread_info->self.reset();
detail::set_current_thread_data(thread_info.get());
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
BOOST_TRY
{
#endif
thread_info->run();
#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
}
BOOST_CATCH (thread_interrupted const&)
{
}
// Removed as it stops the debugger identifying the cause of the exception
// Unhandled exceptions still cause the application to terminate
// BOOST_CATCH(...)
// {
// throw;
//
// std::terminate();
// }
BOOST_CATCH_END
#endif
detail::tls_destructor(thread_info.get());
detail::set_current_thread_data(0);
boost::lock_guard<boost::mutex> lock(thread_info->data_mutex);
thread_info->done=true;
thread_info->done_condition.notify_all();
return 0;
}
}
}