线程无法正常工作并出现错误:启用多线程以使用std :: thread:不允许进行操作 [英] Thread doesn't work with an error: Enable multithreading to use std::thread: Operation not permitted

查看:670
本文介绍了线程无法正常工作并出现错误:启用多线程以使用std :: thread:不允许进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在系统上创建并执行了一个简单的线程.执行此程序时,出现错误消息:启用多线程以使用std :: thread:不允许进行操作

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

有关我的系统的一些详细信息:

some details about my system :

  • Linux ubuntu 13.10
  • g ++ 4.8.1

我编译包括库pthread

源代码:

#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;
}

推荐答案

似乎您正在尝试使用C ++ 11线程.如果是真的,那么

It seems that you are trying to use C++11 threads. If it is true, then

  1. 正确的#include <thread>#include <iostream>,即不要在这些行中使用"并在其前面添加#.
  2. 使用g++ -std=c++11 q.cpp -lpthread进行编译(依赖顺序对于较新的g ++很重要)
  1. correct #include <thread> and #include <iostream>, i.e. do not use " in these lines and add # in front of them.
  2. compile with g++ -std=c++11 q.cpp -lpthread (dependency order matters for newer g++)

提示:当您在静态链接库中使用线程并在可执行文件中使用该库时,则必须将标志-pthread添加到可执行文件的链接命令中.示例:

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:

g++ Obj1.o Obj2.o MyStaticLib.a -o MyExecutable -pthread

这篇关于线程无法正常工作并出现错误:启用多线程以使用std :: thread:不允许进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆