I have two methods that I want to execute in parallel, data are passed by const reference.
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 using the <thread> header, but i had to wait for both methods to finish after joining them.
How can i achieve this type of parallel processing with such a collaborative mecanisme ?.
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.
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
volatilemodifier. Here how the thread function may look if you have to do work in a loop: