1

I'm experiencing a bit of strange behaviour when I try to compile any program that uses multithreading in CodeLite.

I get the error:

terminate called after throwing an instance of 'std::system_error'
  what(): Enable multithreading to use std::thread: Operation not premitted.

after some quick googling I found out I had to add "-pthread" to the compiler options.

Image of Codelite Options. Linker Options

Note: that CodeLite puts -l in front of libraries, so it does use -lpthread

After I clean, and rebuild the project, I still get the error though. As far as I can tell the build log looks fine?

Image of CodeLite build log.

The truly frustrating part comes about when I compile it manually via the command line, It works just fine.

Compiling manually works

I've searched, but none of the solutions seem to work for me? perhaps I've missed a step somewhere?

here's my test code. I should also note I'm using Ubuntu14.04, and CodeLite 9.1.0

#include <iostream>
#include <string>

#include <thread>

void test()
{
    std::cout << " Look it works! \n";
}

void int main(int argc, char** argv)
{
    std::thread thrd_1 = std::thread(test);
    thrd_1.join();

    return 0;
}

Any help would be greatly appreciated!

Dusty
  • 413
  • 6
  • 17
  • You are linking to pthread in your IDE? I use: `-pthread -lpthread -Wl,--no-as-needed`.. Your command line has `-lpthread` but your screenshot of your IDE doesn't show the linker options. – Brandon Mar 13 '16 at 13:32
  • @Brandon Ah, sorry I am also linking `-lpthread` I'll do a quick edit. I have tried `-Wl,--no-as-needed` as a compiler option as well? maybe I put it in the wrong place? – Dusty Mar 13 '16 at 13:42

1 Answers1

1

You are passing -pthread in the compiler options. You need to pass it in the linker options, and in the linker options you do not need to specify pthread as a library. The -pthread option means do whatever it is that links the posix threads library on this platform.

$ g++ -c -O0 -std=c++11 -o main.o main.cpp
$ g++ -o threadtest -pthread main.o
$ ./threadtest
 Look it works!
Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182