Простейший код для воспроизведения этой ошибки и способы ее исправления:
Поместите это в файл с именем s.cpp:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <thread>
using namespace std;
void task1(std::string msg){
cout << "task1 says: " << msg;
}
int main(){
std::thread t1(task1, "hello");
usleep(1000000);
t1.detach();
}
Скомпилируйте так:
el@apollo:~/foo7$ g++ -o s s.cpp -std=c++0x
Запустите это так, ошибка происходит:
el@apollo:~/foo7$ ./s
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Чтобы исправить это, скомпилируйте это так с флагом -pthread:
g++ -o s s.cpp -std=c++0x -pthread
./s
Тогда все будет работать правильно:
task1 says: hello