44

Tried the following example compiled with g++ -std=gnu++0x t1.cpp and g++ -std=c++0x t1.cpp but both of these result in the example aborting.

$ ./a.out 
terminate called after throwing an instance of 'std::system_error'
  what():  
Aborted

Here is the sample:

#include <thread>
#include <iostream>

void doSomeWork( void )
{
    std::cout << "hello from thread..." << std::endl;
    return;
}

int main( int argc, char *argv[] )
{
    std::thread t( doSomeWork );
    t.join();
    return 0;
}

I'm trying this on Ubuntu 11.04:

$ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

Anyone knows what I've missed?

Xeo
  • 129,499
  • 52
  • 291
  • 397
Stéphane
  • 19,459
  • 24
  • 95
  • 136
  • 1
    Maybe the problem is that you didn't join the thread in main? – Armen Tsirunyan Jun 26 '11 at 18:00
  • t.join() was in my example at home, but I missed it when I created the question on StackOverflow. I've edited the sample above. – Stéphane Jun 26 '11 at 19:51
  • I thought it was going to be fine but I can't even get it to compile on g++ MinGW-w64! Seems I'm worse off than you. The following command proves that the declaration for std::thread doesn't get included: `g++ -pthread -std=c++11 -E c:\temp\test.cpp>delme.txt` – doug65536 Feb 11 '13 at 17:11
  • *This problem happened to me when running tensorflow jobs. I waited a few minutes, ran the job again and the error faded away. – Mona Jalal Mar 08 '18 at 21:38

5 Answers5

46

You have to join std::threads, just like you have to join pthreads.

int main( int argc, char *argv[] )
{
    std::thread t( doSomeWork );
    t.join();
    return 0;
}

UPDATE: This Debian bug report pointed me to the solution: add -pthread to your commandline. This is most probably a workaround until the std::thread code stabilizes and g++ pulls that library in when it should (or always, for C++).

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Huh, I could have sworn this worked when I tested it three months ago. I just tested my own correction on Arch Linux GCC 4.6 and I get the same error. That's a bit puzzling :( – rubenvb Jun 26 '11 at 18:18
  • Hmmm, that's strange. I suppose it could simply be a problem with GCC itself that hasn't been worked out yet. It looks like such a simple thing should be working – Ken Wayne VanderLinde Jun 26 '11 at 18:24
  • 2
    I've added `-pthread` but getting same error. I use GCC 4.6 on Ubuntu 11.10 – sorush-r Feb 08 '12 at 17:40
  • 1
    Adding `-pthread` or linking against libpthread (`-lpthread`) is necessary on RHEL 6.3 with gcc 4.4.6 also. – Paul Coccoli Nov 21 '12 at 13:36
  • Scratch that, I just noticed question is explicit that g++ was used. – doug65536 Feb 11 '13 at 16:58
  • Surely it's not strictly true that "you have to join", as you might opt instead to detach? – Tommy Sep 21 '17 at 17:13
  • I observe the same problem with gcc 7.3.0 . adding -pthread does not solve the issue. In any case, I don't want to join thread, since a gui loop is running in my application on the main thread. – sancelot Apr 14 '18 at 19:13
  • I am confused as to whether or not this is an actual bug with gcc 7.3.0, i wrote code similar to the one above and it was working fine before upgrading to gcc 7.3.0, and now my entire project is broken. – asdf Apr 15 '18 at 13:05
13

Please use the pthread library during the compilation: g++ -lpthread.

SarB
  • 131
  • 1
  • 2
3

Simplest code to reproduce that error and how to fix:

Put this in a file called s.cpp:

#include <iostream>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <thread>
using namespace std;
void task1(std::string msg){
  cout << "task1 says: " << msg;
}
int main(){
    std::thread t1(task1, "hello");
    usleep(1000000);
    t1.detach();
}

Compile like this:

el@apollo:~/foo7$ g++ -o s s.cpp -std=c++0x

Run it like this, the error happens:

el@apollo:~/foo7$ ./s
terminate called after throwing an instance of 'std::system_error'
  what():  Operation not permitted
Aborted (core dumped)

To fix it, compile it like this with the -pthread flag:

g++ -o s s.cpp -std=c++0x -pthread
./s

Then it works correctly:

task1 says: hello
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
0

For what it's worth, I had different issue with similar code using threads in g++ (MinGW). Work-around was to put some "delay" between creating a thread and joining it.

Code with infrequently failing assertion:

std::atomic_bool flag{false};
std::thread worker( [&] () { flag.store(true); } );
worker.join();
assert(flag.load()); // Sometimes fails

Work-around:

std::atomic_bool flag{false};
std::thread worker( [&] () { flag.store(true); } );
while (not flag.load()) { std::this_thread::yield(); }
worker.join();
assert(flag.load()); // Works fine

Note that yield() alone did not help, hence the while loop. Using sleep_for(...) also works.

Community
  • 1
  • 1
Petr Vepřek
  • 1,547
  • 2
  • 24
  • 35
-4

You need to link to run time library