Enable multithreading to use std::thread: Operation not permitted问题解决

时间:2024-01-02 19:58:41

在用g++ 4.8.2编译C++11的线程代码后,运行时遇到了如下报错:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

我们先看一下代码:

 #include <boost/atomic.hpp>
#include <thread>
#include <iostream> boost::atomic<int> a{}; void thread()
{
++a;
} int main()
{
std::thread t1{thread};
std::thread t2{thread};
t1.join();
t2.join();
std::cout << a << '\n';
}

之前用的编译命令为:

/opt/compiler/gcc-4.8.2/bin/g++ test.cpp -I ./boost/include -std=c++11

需要改为:

/opt/compiler/gcc-4.8.2/bin/g++ test.cpp -I ./boost/include -std=c++11 -lpthread -Wl,--no-as-needed

这样运行就不会报错了。

本文参考自:https://*.com/questions/19463602/compiling-multithread-code-with-g#