DEALINGS IN THE SOFTWARE.
*/
-#ifndef THREADING_EVENT_H
-#define THREADING_EVENT_H
-
-#ifdef _WIN32
- #include <windows.h>
-#else
- #include "threading/semaphore.h"
-#endif
-
-
-class Event {
+#pragma once
+
+#include <condition_variable>
+
+/** A syncronization primitive that will wake up one waiting thread when signaled.
+ * Calling @c signal() multiple times before a waiting thread has had a chance
+ * to notice the signal will wake only one thread. Additionally, if no threads
+ * are waiting on the event when it is signaled, the next call to @c wait()
+ * will return (almost) immediately.
+ */
+class Event
+{
public:
-#ifdef _WIN32
- Event() { event = CreateEvent(NULL, false, false, NULL); }
- ~Event() { CloseHandle(event); }
- void wait() { WaitForSingleObject(event, INFINITE); }
- void signal() { SetEvent(event); }
-#else
- void wait() { sem.wait(); }
- void signal() { sem.post(); }
-#endif
+ void wait();
+ void signal();
private:
-#ifdef _WIN32
- HANDLE event;
-#else
- Semaphore sem;
-#endif
+ std::condition_variable cv;
+ std::mutex mutex;
+ bool notified = false;
};
-
-#endif
-