[cpp]  view plain  copy

  1. #include<iostream>  
  2. #include<thread>  
  3. void hello()  
  4. {  
  5.     std::cout<<“hello concurrent world\n”;  
  6. }  
  7. int main()  
  8. {  
  9.     std::thread t(hello);  
  10.     t.join();  
  11. }  

C++ -4.8-std = C++11 -lpthread *. CPP

terminate called after throwing an instance of ‘std::system_error’

  what():  Enable multithreading to use std::thread: Operation not permitted\

Aborted (core dumped)

After a long time to find the problem, is a compiler problem, compile time to add options

-Wl,--no-as-needed
Copy the code

Since GCC4.6, the AS-needed option has been automatically added to LD. So the compile option should be: g++ -wl,–no-as-needed -std=c++ 11-pthread main.cpp

or

G++ -std=c++11 -o main-wl,–no-as-needed main.cpp-lpthread \

If you are using QtCreator, you need to add the following in the. Pro file:

LIBS += -pthread
QMAKE_CXXFLAGS += -pthread
QMAKE_CXXFLAGS += -std=c++11
Copy the code

Refer to the article: stackoverflow.com/questions/1…