当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程



我已经并行启动了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;
}
}
}

最新更新