Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;11异步seg故障_C++_Multithreading_Asynchronous_C++11 - Fatal编程技术网

C++ 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 守

我只是在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
守则:

#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
中的符号,只有您碰巧使用了这些功能。因此,如果您忘记链接,运行时将无法解析这些符号,将您的环境视为不支持线程,并引发异常(您没有捕捉到,它会中止您的应用程序)