Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling multithread code with Code::Blocks GNU compiler

The error

I am trying to use std::thread but I have this error when I try to run it.

terminate called after throwing an instance of 'std::system_error'

what(): Enable multithreading to use std::thread: Operation not permitted

Aborted (core dumped)

Result of my research

There are a lot of questions about it and every answer say similar things : I have to build with "-pthread" or "-lpthread". Some also say to add "-Wl,--no-as-needed".

Link Link Link Link Link Link Link

I tried a lot of things but none worked.

Details

I am compiling with Code::Blocks 12.11, GNU GCC Compiler on Lubuntu. In the compiler settings menu, compiler flags I have checked

"Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"

and under other options I wrote what the answer was saying, here is an example

-pthread
-Wl,--no-as-needed

Here is the build log I have (I am not sure if it is important)

g++ -Wall -fexceptions  -std=c++11 -g -pthread -Wl,--no-as-needed  -std=c++11   -I../DeskManagerDll -I/usr/include/X11/extensions -I/usr/include/X11  -c /home/julien/Documents/test/main.cpp -o obj/Debug/main.o
g++ -L/home/julien/Documents/DeskManagerDll -L-L/usr/lib/i386-linux-gnu  -o bin/Debug/test obj/Debug/main.o   -L/usr/X11R6/lib  -lX11 -lXext -lpthread -Wl,--no-as-needed  /home/julien/Documents/DeskManagerDll/bin/Debug/libDeskManagerDll.so 
Output size is 187,15 KB

My question

What am I doing wrong ? What did I miss ?

Edit

I made a very simple program to exclude any other problem.

#include <thread>

void test()
{
    
}

int main()
{
    std::thread thread_fct (test);
    return 0;
}

The build log with this program:

g++ -Wall -fexceptions  -std=c++11 -g -pthread -Wl,--no-as-needed  -std=c++11    -c /home/julien/Documents/test/main.cpp -o obj/Debug/main.o
g++  -o bin/Debug/test obj/Debug/main.o    

I still have the exact same error. I really don't know what to try. Do you have any idea ?

like image 490
Heckel Avatar asked May 15 '14 12:05

Heckel


People also ask

Is code compilation multithreaded?

If you just talk about C# code compilation, that was already answered: no, the C# compiler itself is single threaded.

What compiler should I use for code blocks?

CodeBlocks is an open-source, cross-platform (Windows, Linux, MacOS), and free C/C++ IDE . It supports many compilers, such as GNU GCC (MinGW and Cygwin) and MS Visual C++. It supports interactive debugging (via GNU GDB or MS CDB).

Can C program multithreaded?

Can we write multithreading programs in C? Unlike Java, multithreading is not supported by the language standard. POSIX Threads (or Pthreads) is a POSIX standard for threads.


2 Answers

Your first problem is that -lpthread is a linker option, so it belongs on the linker line (the second command) rather than the compilation line. (Note that the order of the parameters can matter; I got it to work by putting -lpthread on last. I also tried using -pthread instead of -lpthread, which did appear to work and was also less sensitive to where it was put on the linker line. But again, it's a linker option, not a compile option.)

After fixing that, I was able to get your program to compile and run, but it exited with a different exception: terminate called without an active exception. To fix this problem, call thread_fct.join(); in main(). (All threads must be joined, detached, or moved-from before they go out of scope, or your program will abort.)

like image 177
Adam H. Peterson Avatar answered Sep 21 '22 01:09

Adam H. Peterson


The way that was useful for me for code::blocks:

  1. Project -> Build options -> Have g++ follow C++11 ISO ... [-std=c+11] (select checkbox)

  2. Project -> Build options -> Linker settings -> Other linker options add there

    -Wl,--no-as-needed
    -pthread
    
like image 26
Alexey Wodka Avatar answered Sep 20 '22 01:09

Alexey Wodka