- 论坛徽章:
- 2
|
在RHEL 7上面(G++ 4.8.5)编译运行下面的程序:
- #include <iostream>
- #include <string>
- #include <thread>
- using namespace std;
- struct N{
- string s;
- N(){}
- ~N(){cout<<"N dtor"<<endl;}
- };
- void f(){
- N n;
- throw 0;
- }
- int main(){
- try{
- thread a(f), b(f);
- a.join();
- b.join();
- }catch(exception& e){
- cout<<e.what()<<endl;
- }
- return 0;
- }
复制代码
输出打印:
- $g++ 2.cpp -std=c++11 -lpthread && ./a.out
- N dtorN dtor
- terminate called after throwing an instance of 'int'
- terminate called recursively
- Aborted
复制代码
问题:
为什么try/catch没有抓住throw出来的int异常,看起来thread函数f内部已经做了stack unwind,因为N的析构函数调用了。但是try/catch似乎没有用,线程terminated。
这是为何? 谢谢。
|
|