#include #include namespace pEp { class Semaphore { std::mutex mtx; std::condition_variable cv; // Atomic types are types that encapsulate a value whose access is guaranteed // to not cause data races and can be used to synchronize memory accesses among // different threads. // To synchronize threads, ALWAYS use types std::atomic_bool _stop; public: Semaphore() : _stop(false) { } void stop() { std::unique_lock lock(mtx); _stop.store(true); } void try_wait() { std::unique_lock lock(mtx); if (_stop.load() == false) { return; } while(_stop.load() == true) { cv.wait(lock); } } void go() { std::unique_lock lock(mtx); _stop.store(false); cv.notify_all(); } }; }