Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ cyclic 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;
};
Comment

PREVIOUS NEXT
Code Example
Cpp :: ue4 set size of widget c++ 
Cpp :: how to calculate marks in C++ 
Cpp :: how to change the default camera speed values opengl 
Cpp :: how to get a section of a string in c++ 
Cpp :: [3,2,4,-1,-4] 
Cpp :: 191. Number of 1 Bits leetcode solution in c++ 
Cpp :: To toggle (flip the status of) the k-th item of the set 
Cpp :: generate random ints and floats 
Cpp :: how to insert variable into string c++ 
Cpp :: c++ set value to inf 
Cpp :: No Index Out of Bound Checking in C++ 
Cpp :: 1047. Remove All Adjacent Duplicates In String solution leetcode in c++ 
Cpp :: c++ CRL multiline string 
Cpp :: stack in c++ data structure 
Cpp :: https://www.google 
Cpp :: how to traverse string like array in cpp 
Cpp :: c++ static array in Klasse 
Cpp :: how to list directory in c++ 
Cpp :: windows install cppcheck 
Cpp :: vector.rbegin() 
Cpp :: is there anything like vector<intx[100] 
Cpp :: converter python to c++ code 
Cpp :: remove element from vector c++ by index 
Cpp :: write c++ code using glbegin and gland() function 
Cpp :: How to get the last element of an array in C++ using std::array 
Cpp :: how a function gives a pointer as parameter c++ 
Cpp :: c++ destructor 
Cpp :: c++ function with parameters 
C :: hello word c 
C :: buble sort c 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =