60

Update in 2022

C++ 17 & 20 now have built in support for multithreading in the standard library. I would suggest using these rather than using the Linux specific pthread library.

Original Question

I wrote a program to test threads on 64 bit kubuntu linux, version 13.04. Actually I robbed the code from someone else who was writing a test program.

#include <cstdlib>
#include <iostream>
#include <thread>

void task1(const std::string msg)
{
    std::cout << "task1 says: " << msg << std::endl;
}

int main(int argc, char **argv)
{
    std::thread t1(task1, "Hello");
    t1.join();

    return EXIT_SUCCESS;
}

I compiled using:

g++ -pthread -std=c++11 -c main.cpp
g++ main.o -o main.out

Then ran:

./main.out

As an aside, when I 'ls -l', main.out shows up in in green text like all executables, but also has an asterisk at the end of its name. Why is this?

Back to the problem in hand: When I ran main.out, an error appeared, which said:

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

Anyone any ideas on how to fix this?

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
  • This question should not be marked duplicated. There are other situation where you can get this error message. Not limited to threading linking issue. – Kemin Zhou Aug 31 '22 at 18:39

1 Answers1

109

You are not linking pthread properly, try below command(note: order matters)

g++  main.cpp -o main.out -pthread -std=c++11

OR

Do it with two commands

g++ -c main.cpp -pthread -std=c++11         // generate target object file
g++ main.o -o main.out -pthread -std=c++11  // link to target binary
billz
  • 44,644
  • 9
  • 83
  • 100
  • 11
    You can do it with two commands. But you must specify `-pthread` both when you compile and when you link. – David Schwartz Jun 24 '13 at 11:14
  • 6
    @billz Would you mind elaborate on why the order of -pthread matters? – hetepeperfan Jun 24 '13 at 11:45
  • you need to compile it first before link it. put `-pthread ` int the end of g++ command will allow g++ link it properly after compiled to .o file – billz Jun 24 '13 at 11:45
  • thus also reading the comment of David Schwartz -pthread is mainly needed at the linking stage, not neccesary while compiling? – hetepeperfan Jun 24 '13 at 11:50
  • @hetepeperfan: It depends what you mean by "necessary". Are you asking what is required for it to be guaranteed to work or are you asking what happens to work, possibly by luck? – David Schwartz Jun 24 '13 at 12:44
  • @DavidSchwartz It's just that to compile to main.cpp to obectfile main.o does need `-pthread` according to your comment but according the answer one might compile main.cpp to main.o without the flag but link main.o to main.out (the executable) with the flag and this is unclear to me. – hetepeperfan Jun 24 '13 at 13:04
  • 2
    The GCC manual entry for `-pthread` specifically says "This option sets flags for both the preprocessor and linker." You should use it when compiling as well as linking. – Jonathan Wakely Dec 01 '13 at 23:13
  • @JonathanWakely thanks for reviewing that. it's also new to me. I've updated my answer. – billz Dec 01 '13 at 23:26
  • -Wl,--no-as-needed at the end fixed the error as someone told in some other forum, as there is some g++ bug in 4.8 – Vivek Kumar Feb 09 '14 at 07:23
  • Why do I have to link against libpthread? I don't want to use the pthread-library, I want to use the new underlying memory model (i.e. the native C++ threads introduced in the new c++ standard)... – mefiX Sep 29 '14 at 12:25
  • @mefiX the new c++ thread lib is a wrapper for pthread. – billz Sep 29 '14 at 22:59