class Latch { uint32_t Count; mutable std::mutex Mutex; mutable std::condition_variable Cond; public: explicit Latch(uint32_t Count = 0) : Count(Count) {} ~Latch() { sync(); } void inc() { std::lock_guard lock(Mutex); ++Count; } void dec() { std::lock_guard lock(Mutex); if (--Count == 0) Cond.notify_all(); } void sync() const { std::unique_lock lock(Mutex); Cond.wait(lock, [&] { return Count == 0; }); } };