92

Hi I am trying to use std::thread with G++. Here is my test code

#include <thread>
#include <iostream>

int main(int, char **){
    std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
    tt.join();
}

It compiles, but when I try to run it the result is:

terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted 
Aborted

My compiler version:

$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

What is wrong with my test code?

UPDATE: I use the following command line to compile and run my code.

$ g++ -std=c++0x test.cpp
$ ./a.out

and I tried

$ g++ -std=c++0x -lpthread test.cpp
$ ./a.out

still the same.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Earth Engine
  • 10,048
  • 5
  • 48
  • 78
  • 7
    @Earth Engine: this SO answer explains why there are no link errors without the pthread library: http://stackoverflow.com/a/6266345/12711 Short answer: `glibc` has do-nothing stubs for many pthread functions. – Michael Burr Dec 27 '11 at 23:06
  • @EarthEngine can you please put the solution in an answer? Specifically that the `-lpthread` must *follow* the source file. – River Oct 04 '17 at 21:25

5 Answers5

107

I think on Linux pthread is used to implement std::thread so you need to specify the -pthread compiler option.

As this is a linking option, this compiler option need to be AFTER the source files:

$ g++ -std=c++0x test.cpp -pthread
Earth Engine
  • 10,048
  • 5
  • 48
  • 78
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I am trying to compile a very simple program using gcc 4.7.1 and I am having the very same "operation not permitted" error. The problem is that I'm already using -pthread flag. Is there any other flag you know about? – Filipe Felisbino Dec 13 '12 at 11:34
  • 6
    I resolved the problem removing the "-static" flag from linker options, don't know why this happens – Filipe Felisbino Dec 13 '12 at 13:10
  • I am wondering why the compiler doesn't give an error when compiling without the -lpthread option. Anybody?? – zeus2 Feb 14 '14 at 14:22
  • I tried the compiler option `-pthread` first, and this didn't fix the problem. Once I added `-lpthread` also, it worked (but I didn't try `-lpthread` by itself) – gbmhunter Mar 21 '14 at 03:45
  • 1
    Under Ubuntu 14.04 g++ --version (g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1) I had to add the -W1, --no-as-needed g++ --std=c++11 -Wl, --no-as-needed -pthread main.cc – Begui Apr 27 '14 at 22:52
  • 1
    `-Wl,--whole-archive -lpthread -Wl,--no-whole-archive` resolves the problem, not `-pthread`. This is link issue [link](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590) According to `man gcc` `-pthread` is just g++ options that adds multithreading support that sets flags for both the preprocessor and linker – Denis Zaikin Jul 07 '15 at 10:19
6

In addition to using -std=c++0x and -pthread you must not use -static.

Bacon
  • 1,814
  • 3
  • 21
  • 36
Bowie Owens
  • 2,798
  • 23
  • 20
5

-std=c++11 -static -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive works together with -static!!!

See here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590#c4

Bacon
  • 1,814
  • 3
  • 21
  • 36
tuvalu
  • 51
  • 1
  • 1
  • It gets super bulky with `--whole-archive`. I found that [another answer](http://stackoverflow.com/a/27774767/673826) for static linking works for me on OpenWRT – mlt Jun 08 '15 at 23:09
2

Here's a simple CMake file for compiling a C++11 program that uses threads:

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)
list(APPEND CMAKE_CXX_FLAGS "-pthread -std=c++11 ${CMAKE_CXX_FLAGS}")
add_executable(main main.cpp)

One way of building it is:

mkdir -p build
cd build
cmake .. && make
Alexander
  • 9,737
  • 4
  • 53
  • 59
1

Try compiling this way in single command:

g++ your_prog.cpp -o your_output_binary -lpthread -std=gnu++11

You can also try C++11 instead of gnu++11. Hope this works.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59