Class Barrier

Synopsis

#include <Source/Falcor/Utils/Threading.h>

class dlldecl Barrier

Description

Simple thread barrier class. TODO: Once we move to C++20, we should change users of Barrier to use std::barrier instead. The only change necessary will be to use std::barrier::arrive_and_wait() in place of Barrier::wait().

Methods

Barrier overload
operator=
wait

Source

Lines 87-125 in Source/Falcor/Utils/Threading.h.

class dlldecl Barrier
{
public:
    Barrier(size_t threadCount, std::function<void()> completionFunc = nullptr)
        : mThreadCount(threadCount)
        , mWaitCount(threadCount)
        , mCompletionFunc(completionFunc)
    {}
    Barrier(const Barrier& barrier) = delete;
    Barrier& operator=(const Barrier& barrier) = delete;
    void wait()
    {
        std::unique_lock<std::mutex> lock(mMutex);
        auto generation = mGeneration;
        if (--mWaitCount == 0)
        {
            if (mCompletionFunc) mCompletionFunc();
            ++mGeneration;
            mWaitCount = mThreadCount;
            mCondition.notify_all();
        }
        else
        {
            mCondition.wait(lock, [this, generation] () { return generation != mGeneration; });
        }
    }
private:
    size_t mThreadCount;
    size_t mWaitCount;
    size_t mGeneration = 0;
    std::function<void()> mCompletionFunc;
    std::mutex mMutex;
    std::condition_variable mCondition;
};





Add Discussion as Guest

Log in