-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleEvent.h
54 lines (44 loc) · 1.03 KB
/
SimpleEvent.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <mutex>
#include <condition_variable>
namespace stdx
{
class SimpleEvent
{
public:
SimpleEvent() : state(false) {}
virtual void setAndNotify()
{
{
std::unique_lock<std::mutex> lock(mutex);
state = true;
}
cv.notify_all();
}
explicit operator bool() const
{
std::unique_lock<std::mutex> lock(mutex);
return state;
}
template<class Rep, class Period>
bool wait(const std::chrono::duration<Rep, Period> &duration)
{
std::unique_lock<std::mutex> lock(mutex);
return cv.wait_for(lock, duration, [this]() { return state; });
}
void wait()
{
std::unique_lock<std::mutex> lock(mutex);
cv.wait(lock, [this]() { return state; });
}
protected:
std::tuple<std::unique_lock<std::mutex>, bool> getLockAndState()
{
return { std::unique_lock<std::mutex>(mutex), state };
}
private:
mutable std::mutex mutex;
std::condition_variable cv;
bool state;
};
}