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// -*- mode: c++; coding: utf-8; tab-width: 4 -*-
// Copyright (C) 2014, Oscar Acena <oscaracena@gmail.com>
// This software is under the terms of GPLv3 or later.
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
class Event {
public:
Event() : _flag(false) {
}
void set() {
{
boost::lock_guard<boost::mutex> lock(_mutex);
_flag = true;
}
_cond.notify_all();
}
void clear() {
boost::lock_guard<boost::mutex> lock(_mutex);
_flag = false;
}
bool wait(uint16_t timeout) {
if (_flag)
return _flag;
boost::unique_lock<boost::mutex> lock(_mutex);
if (timeout >= 0) {
boost::system_time const ts =
boost::get_system_time() +
boost::posix_time::milliseconds(timeout * 1000);
try {
_cond.timed_wait(lock, ts);
} catch(...) {
std::cout << "ERROR" << std::endl;
std::cout.flush();
}
}
else {
while (!_flag)
_cond.wait(lock);
}
return _flag;
}
private:
bool _flag;
boost::mutex _mutex;
boost::condition_variable _cond;
};