0

我已经并行启动了 5 个调用特定函数的线程。如果其中一个返回某个特定值,我需要终止所有其他剩余线程的进一步处理,并且在onMessage()方法中异步调用特定函数。

4

1 回答 1

1

将 a 传递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;
        }
    }
}
于 2020-02-19T05:57:22.977 回答