C ++ 11:Mac上的Clang没有捕获std :: thread函数引发的异常吗?

时间:2017-08-11 12:48:30

标签: multithreading c++11 exception dynamic throw

我有一个非常简单的程序:

#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;
}

在我的mac + clang环境中,运行结果为:

libc++abi.dylib: terminating with uncaught exception of type int
Abort trap: 6

它不打印&#34; N dtor&#34;正如我所料。所以我的问题是,如果std :: thread函数抛出异常,如何捕获/处理它?不能保证线程函数内的代码不会抛出任何异常。

我在linux上尝试过,可以捕获并打印异常:

Enable multithreading to use std::thread: Operation not permitted

非常感谢。

0 个答案:

没有答案
相关问题