[cpp] view plain copy
- #include<iostream>
- #include<thread>
- void hello()
- {
- std::cout<<“hello concurrent world\n”;
- }
- int main()
- {
- std::thread t(hello);
- t.join();
- }
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…