#pragma once #include #include #include class barrier { private: std::mutex mutex_; uint32_t waiting_count_; const uint32_t barrier_size_; sem_t semaphore_; barrier(const barrier& other) = delete; public: barrier(const uint32_t size); ~barrier(); void wait(); void signal(); }; inline barrier::~barrier() { sem_destroy(&semaphore_); } inline barrier::barrier(const uint32_t size) : barrier_size_(size) { sem_init(&semaphore_, 0, 0); waiting_count_ = 0; } inline void barrier::wait() { mutex_.lock(); if (++waiting_count_ >= barrier_size_) { for (uint32_t i = 1; i < waiting_count_; i++) sem_post(&semaphore_); mutex_.unlock(); } else { mutex_.unlock(); sem_wait(&semaphore_); } }