c++11 多线程 Enable multithreading to use std::thread: Operation not permitted
#include <iostream>
#include <stdio.h>
#include <future>
#include <mutex>
#include <condition_variable>
using namespace std;
std::mutex _mutex;
std::condition_variable cv;
bool stop=false;
void test()
{
cout << "Hello world!" << endl;
}
void polling(std::chrono::seconds s)
{
while(true)
{
std::unique_lock<std::mutex> lk(_mutex);
cv.wait_for(lk,s,[]{return stop;});
if(!stop)
test();
lk.unlock();
if(stop)
break;
}
}
int main()
{
std::chrono::seconds s(5);
std::async(std::launch::async, polling,s);
char ch;
ch=cin.get();
if(ch=='e')
{
std::lock_guard<std::mutex> lk(_mutex);
stop = true;
cv.notify_one();
}
return 0;
}
opensuse13.1下code::blocks 13.12
在code::blocks的build option中添加了-pthread,other link option中添加了-Wl,--no-as-needed,编译通过后在控制台执行时发生Enable multithreading to use std::thread: Operation not permitted错误。
目的:每5秒打印hello world,按e结束
求解
多谢!