Author Topic: pause causing multithreaded programs to fail  (Read 5724 times)

Offline bobbaluba

  • Single posting newcomer
  • *
  • Posts: 2
pause causing multithreaded programs to fail
« on: October 01, 2011, 06:17:15 pm »
I'm testing the new std::thread with this program

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

using namespace std;

int threads=0;

void countToInfinity(){
    int thread = threads++;
    for(int i=0; i<100; i++){
        cout << thread << ": " << i << endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(rand()%10));
    }
    std::this_thread::sleep_for(std::chrono::milliseconds(10000));
}

int main()
{
    cout << "bu";
    std::thread my_thread(countToInfinity);
    std::thread my_thread2(countToInfinity);
    my_thread.join();
    my_thread2.join();
    return 0;
}

int project -> project properties -> build targets -> pause when execution ends
checking this checkbox causes my program to fail with:

Code
terminate called after throwing an instance of 'std::system_error'
  what():
aborted

Process returned 134(0x86)

I've verified that the program itself works correctly by running it from the command line.

I'm using ubuntu with g++ 4.5.2 codeblocks 10.05

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: pause causing multithreaded programs to fail
« Reply #1 on: October 01, 2011, 06:52:24 pm »
If you search the web for the error-message, you will find the solution.

Your problem is not related to C::B, nor has it anything to do with our wrapper, that runs the executable.
Your code does not work inside C::B or from commandline, if you do not use the correct parameters.

Add "-pthread" to the linker-settings should do the trick.

By the way:
not searching the forum and the web violates our forum rules.

With registering here, you accepted these rules and should respect them.

Offline bobbaluba

  • Single posting newcomer
  • *
  • Posts: 2
Re: pause causing multithreaded programs to fail
« Reply #2 on: October 01, 2011, 08:13:08 pm »
I did search the forums and the web.
And i did correctly add the -pthread in my make file
It appears the problem however, occured when i wrote my own makefile. I did this because i wasn't sure how to do add the -pthread flag with the cb menus.
I didn't update the output filename however, so code::blocks still launched my old executable without the -pthread from the debug folder, although it linked and placed the new one with -pthread in the project root.

This still doesn't explain why the program exits with status 0 when i uncheck the pause checkbox

Sorry for the inconvenience, i will probably never post here again.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: pause causing multithreaded programs to fail
« Reply #3 on: October 01, 2011, 08:33:34 pm »
I did search the forums and the web.
And i did correctly add the -pthread in my make file
It appears the problem however, occured when i wrote my own makefile. I did this because i wasn't sure how to do add the -pthread flag with the cb menus.
I didn't update the output filename however, so code::blocks still launched my old executable without the -pthread from the debug folder, although it linked and placed the new one with -pthread in the project root.
Not C::B builds, links and places the executable if you use a custom makefile !
C::B does not know anything about the content of the makefile.
You can fix the place where C::B looks for the executables in "Project -> Properties -> Build targets -> Output filename".
If it points to the correct place, C::B can launch it.

You did not tell anything about a custom makefile in your original post, neither did you tell anything about the solutions you tried.
If you would have told us, that you want to add -pthread, but don't know where to do it, it would be easy to help you.

There are two places where it is probably needed: "Project -> Build options -> Compiler Settings -> Other options" and "Project -> Build options -> Linker settings -> Other linker settings", the last one is needed in any case, the first one seems to be unneeded for your testcase, but is most likely needed for more complex programs.

This still doesn't explain why the program exits with status 0 when i uncheck the pause checkbox
In both cases (without -pthread), the "Build log" shows the return code of 0, because it's the return code of the terminal that was called to run the executable in, it does not care about the programs return code (not a C::B issue, and absolutely correct if the terminal-emulator exits normally).
If you have checked "Pause when ...", the executable is run inside the terminal through a wrapper-program, that keeps the terminal open, so you can see the output, and that prints the return-code of your exe.
The "Build log" still says it's 0, becaue it still shows the return code of the terminal.
So everything is okay here.

Sorry for the inconvenience, i will probably never post here again.
That's your decision.