Java 的循环屏障的 C++ 版本是什么?

What is C++ version of Java's cyclic barrier?

在 java 中,多个线程可以在某个时间点等待所有其他线程,这样它们就不会在所有其他线程完成第一个块之前开始新的代码块:

CyclicBarrier barrier = new CyclicBarrier(2);

// thread 1
readA();
writeB();
barrier.await();
readB();
writeA();

// thread 2
readA();
writeB();
barrier.await();
readB();
writeA();

是否有准确或简单的 C++ 转换?

还有OpenCL,也有类似的指令:

readA();
writeB();
barrier(CLK_GLOBAL_MEM_FENCE);
readB();
writeA();

因此所有相邻线程都相互等待,但这只是一个受约束的 C 实现。

C++ STL 没有循环屏障。您可以向标准委员会提出一个建议:)

像 Oracle 或 Microsoft 这样的公司可以快速决定将什么添加到他们的语言库中。对于 C++,人们必须达成一致,这可能需要一段时间。

256 个线程很多。对于所有与性能相关的问题,您需要衡量代码以做出明智的决定。对于 256 个线程,我很想使用由第 11 个屏障同步的 10 个屏障。您需要测量才能知道这是否真的更好。

查看我的循环障碍的 C++ 实现,灵感来自 Java。我几年前写的。它基于我在 http://studenti.ing.unipi.it/~s470694/a-cyclic-thread-barrier/ 找到的其他人的(错误)代码(link 不再工作...)代码非常简单(无需相信我)。当然,它是原样,没有任何保证。

// 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;
};

您可以在 boost 库中找到它,它被称为 barrier 并且缺少等待超时选项。

  • C++20 现在有 std::barrier

  • POSIX 有“pthread_barrier_t”,界面如下:

     int pthread_barrier_init(pthread_barrier_t *restrict barrier, 
           const pthread_barrierattr_t *restrict attr, unsigned count); 
    
     int pthread_barrier_wait(pthread_barrier_t *barrier);
     int pthread_barrier_destroy(pthread_barrier_t *barrier);