How do I terminate/stop all other thread when I catch specific value in one of the thread execution (in activemq cpp)?

66 Views Asked by At

I have parallely launched 5 threads which are calling specific function. If one of them returns some specific value, I need to terminate the further processing of all other remaining threads and the specific function is called asynchronously in the onMessage() method.

1

There are 1 best solutions below

2
JohnFilleau On

Pass a std::atomic<bool>& terminate_flag to all launched threads. When one process find the searched for value, update the terminate_flag. All threads should be running an infinite loop? Modify the loop to exit when the terminate_flag gets set to true.

void thread_function(std::atomic<bool>& terminate_flag, T& search_value, ...)
{
    while(terminate_flag == false)
    {
        ...do stuff...
        if (found_value == search_value)
        {
            terminate_flag = true;
        }
    }
}