C++ 线程不工作并出现错误:启用多线程以使用 std::thread:不允许操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19618926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Thread doesn't work with an error: Enable multithreading to use std::thread: Operation not permitted
提问by user2925158
I created and executed a simple thread on my system. when I execute this program, I have the error message : Enable multithreading to use std::thread: Operation not permitted
我在我的系统上创建并执行了一个简单的线程。当我执行此程序时,出现错误消息:启用多线程以使用 std::thread:不允许操作
some details about my system :
关于我的系统的一些细节:
- linux ubuntu 13.10
- g++ 4.8.1
- linux Ubuntu 13.10
- 克++ 4.8.1
I compile the source code including the library pthread
我编译了包含库的源代码 pthread
The source code:
源代码:
#include <iostream>
#include <thread>
using namespace std;
void func(void) {
cout << "test thread" << endl;
}
int main() {
cout << "start" << endl;
thread t1 (func);
t1.join();
cout << "end" << endl;
return 0;
}
回答by Igor Popov
It seems that you are trying to use C++11 threads. If it is true, then
您似乎正在尝试使用 C++11 线程。如果是真的,那么
- correct
#include <thread>
and#include <iostream>
, i.e. do not use"
in these lines and add#
in front of them. - compile with
g++ -std=c++11 q.cpp -lpthread
(dependency order matters for newer g++)
- 正确
#include <thread>
和#include <iostream>
,即不要"
在这些行中使用并#
在它们前面添加。 - 编译
g++ -std=c++11 q.cpp -lpthread
(依赖顺序对于较新的 g++ 很重要)
Hint: when you are using threads in a static linked library and use this library in an executable, then you have to add the flag -pthread
to the link command for the executable. Example:
提示:当您在静态链接库中使用线程并在可执行文件中使用该库时,您必须将标志添加-pthread
到可执行文件的链接命令中。例子:
g++ Obj1.o Obj2.o MyStaticLib.a -o MyExecutable -pthread