我已经并行启动了5个线程,它们正在调用特定的函数。如果其中一个返回特定值,我需要终止对所有其他线程的进一步处理,并且在onMessage((方法中异步调用特定函数。
将std::atomic<bool>& terminate_flag
传递给所有启动的线程。当一个进程找到搜索到的值时,更新terminate_flag
。所有线程都应该运行一个无限循环?当terminate_flag
设置为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;
}
}
}