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

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

本文关键字:版本 是什么 C++ 循环 Java      更新时间:2023-10-16

在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 个线程,我会倾向于使用 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;
};
  • 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);
    

你可以在 boost 库中找到它,它被称为屏障,缺少等待超时选项。