C++ STL 没有循环障碍。您可以向标准委员会提出一项建议:)
像甲骨文或微软这样的公司可以快速决定将什么添加到他们的语言库中。对于 C++,人们必须达成一致,这可能需要一段时间。
256个线程很多。与所有与性能相关的问题一样,您需要衡量代码以做出明智的决定。对于 256 个线程,我很想使用 10 个由第 11 个屏障同步的屏障。您需要测量以了解这是否实际上更好。
看看我受 Java 启发的循环障碍的 C++ 实现。我是几年前写的。它基于我在http://studenti.ing.unipi.it/~s470694/a-cyclic-thread-barrier/找到的其他人的(错误的)代码(链接不再起作用......)代码真的很简单(无需相信我)。当然,它就是这样,没有任何保证。
// Modeled after the java cyclic barrier.
// Allows n threads to synchronize.
// Call Break() and join your threads before this object goes out of scope
#pragma once
#include <mutex>
#include <condition_variable>
class CyclicBarrier
{
public:
explicit CyclicBarrier(unsigned numThreads)
: m_numThreads(numThreads)
, m_counts{ 0, 0 }
, m_index(0)
, m_disabled(false)
{ }
CyclicBarrier(const CyclicBarrier&) = delete;
CyclicBarrier(CyclicBarrier &&) = delete;
CyclicBarrier & operator=(const CyclicBarrier&) = delete;
CyclicBarrier & operator=(CyclicBarrier &&) = delete;
// sync point
void Await()
{
std::unique_lock<std::mutex> lock(m_requestsLock);
if (m_disabled)
return;
unsigned currentIndex = m_index;
++m_counts[currentIndex];
// "spurious wakeup" means this thread could wake up even if no one called m_condition.notify!
if (m_counts[currentIndex] < m_numThreads)
{
while (m_counts[currentIndex] < m_numThreads)
m_condition.wait(lock);
}
else
{
m_index ^= 1; // flip index
m_counts[m_index] = 0;
m_condition.notify_all();
}
}
// Call this to free current sleeping threads and prevent any further awaits.
// After calling this, the object is no longer usable.
void Break()
{
std::unique_lock<std::mutex> lock(m_requestsLock);
m_disabled = true;
m_counts[0] = m_numThreads;
m_counts[1] = m_numThreads;
m_condition.notify_all();
}
private:
std::mutex m_requestsLock;
std::condition_variable m_condition;
const unsigned m_numThreads;
unsigned m_counts[2];
unsigned m_index;
bool m_disabled;
};