C++, Running two independent function in parallel

I have two methods that I want to execute in parallel, they are completely independent and do not share memory, the only common point between them are the parameters.

As soon as one of the method has finished its job, it is not necessary for the other to continue, since one of them can have a long execution time depending the entry and therefore must stop.

I found that I could execute the two methods in parallel by running two threads, I used the <thread> header to do it, but i had to wait for both methods to finish using .join().

How can i achieve this type of parallel processing with such a collaborative mecanisme ?.

Answers


I wrote a small sample to show how it is done. It is not archivable with join, as you already found out. You need an event you can signal from a thread, when it has the result. For that, you have to use std::conditon_variable. The sample shows the minimal possible solution for your described problem. In the sample, the result is a simple in.

There are two pitfalls you have to care.

a.The thread is finished before the main is waiting for. For that reason I lock the mutex before I start the threads.

b. The result is overwritten. I managed that by testing the result before writing it.

#include <thread>
#include <condition_variable>
#include <mutex>

std::mutex mtx;
std::condition_variable cv;

int result = -1;

void thread1()
{
    // Do something
    // ....
    // ....

    // got a result? publish it!
    std::unique_lock<std::mutex> lck(mtx);
    if (result != -1)
        return; // there is already a result!

    result = 0; // my result
    cv.notify_one(); // say I am ready
}

void thread2()
{
    // Do something else
    // ....
    // ....

    // got a result? publish it!
    std::unique_lock<std::mutex> lck(mtx);
    if (result != -1)
        return; // there is already a result!

    result = 1; // my result
    cv.notify_one(); // say I am ready
}

int main(int argc, char * argv[])
{
    std::unique_lock<std::mutex> lck(mtx); // needed so the threads cannot finish befor wait
    std::thread t1(thread1), t2(thread2);

    cv.wait(lck); // wait until one result

    // here result is 0 or 1;

    // If you use the loop described below, you can use join safely:
    t1.join();
    t2.join();
    // You have to call join or detach of std::thread objects before the
    // destructor of std::thread is called. 

    return 0;
}

If you want to stop the other thread if one has already a result, the only legit way is to test the result frequently in both threads an stop if someone already has the result. In this case, if you use a pointer or or generic type, you should mark it with the volatile modifier. Here how the thread function may look if you have to do work in a loop:

void thread1()
{
    // Do something
    bool bFinished=false;
    while(!bFinished)
    {
      { // this bracket is necessary to lock only the result test. Otherwise you got a deadlock a forced sync situation.
        std::unique_lock<std::mutex> lck(mtx);
        if (result != -1)
           return; // there is already a result!
      }         
      // do what ever you have to do
    }

    // got a result? publish it!
    std::unique_lock<std::mutex> lck(mtx);
    if (result != -1)
        return; // there is already a result!

    result = 0; // my result
    cv.notify_one(); // say I am ready
}

Need Your Help

Detect if context is from activity or adapter

java android android-activity android-context onactivityresult

Is there any ability to detect if context is from activity or adapter?

Set SVG height and width to that of the <text> node inside it

html css svg height inline

Is there a way to make the width and height of an SVG element equal to the width of the text inside a text node inside it?