37

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

I compile the source code including the library 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;
}
jbsu32
  • 1,036
  • 1
  • 11
  • 31
user2925158
  • 381
  • 1
  • 3
  • 3
  • 1
    I removed the `java` tag and added `C/Cpp`... – home Oct 27 '13 at 14:06
  • 3
    @home Why did you add C? This is C++, not C... –  Oct 27 '13 at 14:11
  • 2
    Oh and BTW this doesn't even compile. Missing hash signs before the preprocessor directives... And there's no indentation. Fix your code. –  Oct 27 '13 at 14:12
  • I have feeling that you want to use new C++11 threads. In order to compile this you need to compile with `-std=c++11` switch. – Igor Popov Oct 27 '13 at 14:14
  • Possible duplicate of [Compiling multithread code with g++](http://stackoverflow.com/questions/19463602/compiling-multithread-code-with-g). – Oswald Oct 27 '13 at 14:18
  • You need to compile with the `-pthread` and `-std=c++11` compiler flags. – juanchopanza Oct 27 '13 at 14:18
  • In g++ 4.8.1, characters of c11 is implicit, try to add `g++ -std=c++11 -pthread` explicitly. – YaleCheung Oct 27 '13 at 14:29
  • 1
    according to the accepted answer in the duplicate, you need these options with gcc 4.8: `-pthread -std=c++11 -Wl,--no-as-needed` – Walter Oct 27 '13 at 14:33
  • @home I didn't ask why you removed the Java tag. I asked why you added the C tag, when this code is clearly not C. –  Oct 27 '13 at 15:55
  • @H2CO3: got it, I just was not sure whether it's about C or Cpp (not my 'core' business)... – home Oct 27 '13 at 15:56
  • In addition to answer 1, check the following link too. http://stackoverflow.com/questions/19463602/compiling-multithread-code-with-g –  Feb 13 '15 at 23:43

1 Answers1

36

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

  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++)

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
Jeremy Visser
  • 6,237
  • 1
  • 25
  • 30
Igor Popov
  • 2,588
  • 17
  • 20
  • 13
    To be clearer: the problem was in missing of __-pthread__ flag of gcc for a moment of compilaion. If it was `#include`s, the code just wouldn't compiled. – Hi-Angel May 14 '14 at 19:29
  • 1
    Why is -pthreads necessary? Is this also needed in non C++11 code? – MrMowgli May 11 '16 at 19:54
  • 1
    Not required by the standard but implementation specific @MrMowgli – ABCD Aug 03 '16 at 01:38