gpt4 book ai didi

c++ - 在线程之间创建 'synchronization point'

转载 作者:行者123 更新时间:2023-11-30 04:26:04 25 4
gpt4 key购买 nike

我有几个 boost::threads,它们都执行相同的功能。

void foo(){
//Lock Mutex
//Do some stuffs, part 1
//Unlock Mutex
//Do some stuffs, part 2
//Lock Mutex
//Do some stuffs, part 3
//Unlock Mutex
}

为了让我的应用程序正常工作,有必要在并行执行第 2 部分之前,所有线程都已完成第 1 部分的执行。我找不到任何可以让我做到这一点的机制……我是不是漏掉了什么?

谢谢。

最佳答案

使用 Boost 障碍。官方文档定义:

A barrier is a simple concept. Also known as a rendezvous, it is a synchronization point between multiple threads. The barrier is configured for a particular number of threads (n), and as threads reach the barrier they must wait until all n threads have arrived. Once the n-th thread has reached the barrier, all the waiting threads can proceed, and the barrier is reset.

摘自here .

关于c++ - 在线程之间创建 'synchronization point',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11769917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com