//Attempt of barrier implementation for case where num_threads*2 -1 >= barrier_count #include #include #include class Barrier { std::mutex mtx; std::condition_variable cv; std::size_t count = 0; std::size_t max_count = 0; bool flushing = false; public: Barrier(size_t max_count) { this->max_count = max_count; } void arrive_and_wait() { std::unique_lock lock(mtx); while (flushing) cv.wait(lock); if (++count < max_count) { while (!flushing) cv.wait(lock); } else { flushing = true; cv.notify_all(); } if (--count == 0) { flushing = false; cv.notify_all(); } } }; // int main() {}