#pragma once #include #include class AutoSemaphore { public: AutoSemaphore() { } virtual ~AutoSemaphore() { notify_all(); } virtual void notify_all() { std::lock_guard lck(mutx); cv.notify_all(); } virtual bool wait(int timeout_ms) { std::unique_lock lck(mutx); return cv.wait_for(lck, std::chrono::milliseconds(timeout_ms)) == std::cv_status::no_timeout; } protected: std::mutex mutx; std::condition_variable cv; }; class AutoEvent : public AutoSemaphore { public: AutoEvent() : bNotify(false) { } virtual ~AutoEvent() { notify_all(); } virtual void notify_all() { std::lock_guard lck(mutx); bNotify = true; cv.notify_all(); } virtual bool wait(int timeout_ms) { std::unique_lock lck(mutx); if (bNotify) { bNotify = false; return true; } bool ret = cv.wait_for(lck, std::chrono::milliseconds(timeout_ms)) == std::cv_status::no_timeout; bNotify = false; return ret; } private: volatile bool bNotify; };