4
我想在線程中拋出一個異常,並允許調用進程來捕獲它。但是,這似乎會導致整個應用程序崩潰。查看測試代碼附帶的永不打印任何退出語句。Boost線程異常處理
1 #include <boost/thread.hpp>
2 #include <iostream>
3
4 void wait(int seconds)
5 {
6 boost::this_thread::sleep(boost::posix_time::seconds(seconds));
7 }
8
9 void thread()
10 {
11 for (int i = 0; i < 5; ++i)
12 {
13 wait(1);
14 std::cout << i << std::endl;
15 }
16 throw;
17 }
18
19 int main()
20 {
21 try
22 {
23 boost::thread t(thread);
24 t.join();
25 std::cout << "Exit normally\n";
26 }
27 catch (...)
28 {
29 std::cout << "Caught Exception\n";
30 }
31 }
這在C++ 11中變得更加簡單(任何異常都可以在std :: exception_ptr中捕獲) – Cubbi
@Cubbi感謝您的信息,我不知道那:) – Ralf