-
Notifications
You must be signed in to change notification settings - Fork 3
/
Event.h
78 lines (57 loc) · 1.46 KB
/
Event.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef AllocatorH
#define AllocatorH
#include <mutex>
#include <condition_variable>
#include <chrono>
namespace Moya {
class Event
{
public:
Event(bool initial, bool manual) :
state(initial), manual(manual)
{
}
void change(bool manual)
{
std::unique_lock<std::mutex> lock(mutex);
this->manual = manual;
}
void set()
{
std::unique_lock<std::mutex> lock(mutex);
if (state)
return;
state = true;
if (manual)
condition.notify_all();
else
condition.notify_one();
}
void reset()
{
std::unique_lock<std::mutex> lock(mutex);
state = false;
}
void wait()
{
std::unique_lock<std::mutex> lock(mutex);
condition.wait(lock, [this] { return state; });
if (!manual)
state = false;
}
template<class Rep, class Period>
void wait(const std::chrono::duration<Rep, Period> &timeout)
{
std::unique_lock<std::mutex> lock(mutex);
if (!condition.wait_for(lock, timeout, [this] { return state; }))
return;
if (!manual)
state = false;
}
private:
std::mutex mutex;
std::condition_variable condition;
bool state, manual;
};
}
#endif