-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandle-waiter.h
68 lines (54 loc) · 1.03 KB
/
handle-waiter.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
/*
** ¶à¾ä±úµÈ´ý¹ÜÀí
** author
*/
#ifndef ULT_HANDLE_WAITER_H_
#define ULT_HANDLE_WAITER_H_
#include <windows.h>
#include <vector>
namespace ult {
class HandleWaiter {
public:
HandleWaiter(void) : count_(0),
all_closed_(true) {
}
~HandleWaiter() {
CloseAll();
}
bool PushBack(HANDLE h) {
if (MAXIMUM_WAIT_OBJECTS == count_) {
return false;
}
harr_[count_++] = h;
all_closed_ = false;
return true;
}
bool WaitAll(int milliseconds) {
DWORD wait_ret = WaitForMultipleObjects(count_, harr_, true, milliseconds);
Clear();
if (WAIT_TIMEOUT == wait_ret || WAIT_FAILED == wait_ret) {
return false;
}
return true;
}
void CloseAll(void) {
if (all_closed_) {
return;
}
for (int i = 0; i < count_; ++i) {
CloseHandle(harr_[i]);
}
all_closed_ = true;
}
void Clear(void) {
CloseAll();
count_ = 0;
}
private:
HANDLE harr_[MAXIMUM_WAIT_OBJECTS];
bool all_closed_;
int count_;
};
}
#endif