C++20 barrier
01 C++20 barrier
下面是在 www.open-std.org
对 C++20 barrier
的一点介绍内容。(semaphores、latch、barrier)
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1135r2.html
1
barrier
屏障是一种线程协调机制,它最多允许阻止线程的预期计数,直到该计数由在其每个连续阶段中到达该屏障的线程(精确地)相加。一旦线程在某个阶段的同步点处从阻塞中释放出来,它们就可以在下一阶段立即重新使用同一屏障。[ 注意:因此,对于管理由多个线程处理的重复任务或较大任务的阶段非常有用。— 尾注 ]
屏障的完成步骤是与屏障的某个阶段关联的一组效果(可能是空的)。当本节中定义的成员函数到达障碍时,它们具有以下作用:
当此阶段的预期线程数到达屏障时,这些线程之一将执行屏障类型的完成步骤。
当完成步骤完成时,在此阶段在同步点被阻塞的所有线程都将被解除阻塞,并且屏障进入其下一阶段。完成步骤的结束很可能发生在所有调用的返回不受其完成阻塞的情况下。
A barrier is a thread coordination mechanism that allows at most an expected count of threads to block until that count is summed (exactly) by threads that arrived at the barrier in each of its successive phases. Once threads are released from blocking at the synchronization point for a phase, they can reuse the same barrier immediately in its next phase. [ Note: It is thus useful for managing repeated tasks, or phases of a larger task, that are handled by multiple threads. — end note ]
A barrier has a completion step that is a (possibly empty) set of effects associated with a phase of the barrier. When the member functions defined in this subclause arrive at the barrier, they have the following effects:
When the expected number of threads for this phase have arrived at the barrier, one of those threads executes the barrier type’s completion step.When the completion step is completed, all threads blocked at the synchronization point for this phase are unblocked and the barrier enters its next phase. The end of the completion step strongly happens before the returns from all calls unblocked by its completion.
barrier的可能定义
namespace std {
template<class CompletionFunction>
class barrier {
public:
using arrival_token = implementation-defined;
explicit barrier(ptrdiff_t expected,
CompletionFunction f = CompletionFunction());
~barrier();
barrier(const barrier&) = delete;
barrier(barrier&&) = delete;
barrier& operator=(const barrier&) = delete;
barrier& operator=(barrier&&) = delete;
[[nodiscard]] arrival_token arrive(ptrdiff_t update = 1);
void wait(arrival_token&& arrival) const;
void sync();
void arrive_and_drop();
private:
CompletionFunction completion; // exposition only
};
}