C++ c++;11异步seg故障
我只是在GCC4.7.2中尝试一些新的C++11特性,不过当我运行seg时,会出现故障C++ c++;11异步seg故障,c++,multithreading,asynchronous,c++11,C++,Multithreading,Asynchronous,C++11,我只是在GCC4.7.2中尝试一些新的C++11特性,不过当我运行seg时,会出现故障 $ ./a.out Message from main. terminate called after throwing an instance of 'std::system_error' what(): Unknown error -1 Aborted (core dumped) 对于c++0x,我使用GCC的“beta”功能进行编译,其中包括: g++ -std=c++11 c11.cpp 守
$ ./a.out
Message from main.
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
对于c++0x,我使用GCC的“beta”功能进行编译,其中包括:
g++ -std=c++11 c11.cpp
守则:
#include <future>
#include <iostream>
void called_from_async() {
std::cout << "Async call" << std::endl;
}
int main() {
//called_from_async launched in a separate thread if possible
std::future<void> result( std::async(called_from_async));
std::cout << "Message from main." << std::endl;
//ensure that called_from_async is launched synchronously
//if it wasn't already launched
result.get();
return 0;
}
#包括
#包括
从\u async()调用\u的void{
std::cout我相信发生这种情况是因为您忘记了链接POSIX线程库。只需将-pthread
或-lpthread
添加到g++
标志中,问题就会消失
如果您对细节感兴趣,这是因为C++11运行时在运行时解析pthread
中的符号,只有您碰巧使用了这些功能。因此,如果您忘记链接,运行时将无法解析这些符号,将您的环境视为不支持线程,并引发异常(您没有捕捉到,它会中止您的应用程序)