gpt4 book ai didi

multithreading - boost 线程异常处理

转载 作者:行者123 更新时间:2023-12-04 18:49:42 26 4
gpt4 key购买 nike

我试图在线程中抛出异常并允许调用进程捕获它。但是,这似乎会导致整个应用程序崩溃。请参阅附加的测试代码从不打印任何退出语句。

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 }

最佳答案

看看 boost 异常:Transporting of Exceptions Between Threads .
这种方法对我很有效。

关于multithreading - boost 线程异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8161962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com