2

I am trying to do a dekker algorithm implementation for homework, I understand the concept but I'm not being able to execute two threads in parallel using C++0x.

#include <thread>
#include <iostream>

using namespace std;

class Homework2 {

public:
    void run() {
        try {
            thread c1(&Homework2::output_one, this);

            thread c2(&Homework2::output_two, this);


        } catch(int e) {
            cout << e << endl;
        }

    }

    void output_one() {
        //cout << "output one" << endl;
    }

    void output_two() {
        //cout << "output two" << endl;
    }


};

int main() {
    try {
        Homework2 p2;
        p2.run();
    } catch(int e) {
        cout << e << endl;
    }
    return 0;
}

My problem is that the threads will return this error:

terminate called without an active exception
Aborted

The only way to success until now for me has been adding c1.join(); c2.join(); or .detach(); the problem is that join(); will wait for the threads to finish, and detach(); ... well Im not sure what detach does because there is no error but also no output, I guess it leaves the threads on their own...

So all this to say: Does anybody knows how can I do this both threads to run parallel and not sequencial?? The help is must appreciated!

Thanks.-

P.S: here is what I do for build:

g++ -o output/Practica2.out main.cpp -pthread -std=c++11 
RicardoE
  • 1,665
  • 6
  • 24
  • 42
  • here is what I do for build: g++ -o output/Practica2.out main.cpp -pthread -std=c++11 – RicardoE Feb 13 '14 at 03:35
  • Don't add additional information in comments. Instead, [edit the original question](http://stackoverflow.com/posts/21744657/edit) to include the (properly formatted) information. – Jonathon Reinhart Feb 13 '14 at 03:38
  • :okay: :S I will edit the post... – RicardoE Feb 13 '14 at 03:41
  • `"the problem is that join(); will wait for the threads to finish"`... if you exit right away, what do you expect your program to actually accomplish? – Jonathon Reinhart Feb 13 '14 at 03:43
  • Hi Jonathon, the homework dictates that the threads must be an infinite loop which stops only at a keystroke, inside the threads Im supposed to block a critical region writing to a single file, using booleans only to control the block. but the threads allways run sequencially.. – RicardoE Feb 13 '14 at 03:47
  • come on! why voting to close my question :( the other thread speaks about the exception, and does not provide clear instructions on how to prevent it... just answers based on theory TT__TT – RicardoE Feb 13 '14 at 03:51
  • 1
    I don't think you understand. The threads are started, and are running. Then you have three threads: main, c1, and c2. If you don't wait on them, your program exits immediately. That question explains why (the destructor being called). – Jonathon Reinhart Feb 13 '14 at 04:26

2 Answers2

3

The only way to success until now for me has been adding c1.join(); c2.join(); or .detach();...

After you have spawned the 2 threads, your main thread continues on and, based on your code, ends 'pretty' quick (p2.run() then return 0; are relatively close in CPU instruction 'time'). Depending on how quickly the threads started, they might not have had enough CPU time to fully 'spawn' before the program terminated or if they did fully spawn, there might not have been enough time to do the proper cleanup by the kernel. This is also known as a race condition.

Calling join on the spawned threads from the thread you spawned them from allows the threads to finish and clean up properly (under the hood) before your program exits (a good thing). Calling detach works in this scenario too as it releases all resources (under the hood) from your thread object, but keeps the thread active. In the case of calling detach there were no errors reported because the thread objects were detached from the executing threads, so when your program exited, the kernel (nicely) cleaned up the threads for you (or at least that's what might happen, depends on OS/compiler implementation, etc.) so you didn't see your threads ending 'uncleanly'.

So all this to say: Does anybody knows how can I do this both threads to run parallel and not sequencial??

I think you might have some confusion on how threads work. Your threads already run in 'parallel' (so to speak), that is the nature of a thread. Your code posted does not have anything that would be 'parallel' in nature (i.e. parallel computing of data) but your threads are running concurrently (at the same time, or 'parallel' to each).

If you want your main thread to continue without putting the join in the run function, that would require a little more code than what you currently have and I don't want to assume how your code's future should look, but you could take a look at these two questions regarding the std::thread as a member of a class (and executing within such).

I hope that can help.

Community
  • 1
  • 1
txtechhelp
  • 6,625
  • 1
  • 30
  • 39
1

Ok this is bit more complex but I will try to explain some things in your code.

When you create the threads in the method called run, you want to print two things (imagine you uncomment the lines), but the thread object is destroyed in the stack unwiding of the method which created them (run).

You actually need to do two things, first create the threads and keep them running(for example do it as pointers) and second call the method join to release all the memory and stuff they needed when they are finished.

You can store you threads in a vector something like std::vector<std::thread*>

Jose Palma
  • 756
  • 6
  • 13
  • I've seen a lot of people storing threads in vectors, I guess it is a good practice. Will keep that in mind. I finally understood the meaning of join(); – RicardoE Feb 13 '14 at 18:28