-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent.cpp
153 lines (122 loc) · 3.54 KB
/
Event.cpp
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* \file Event.cpp
* \brief
*
* \review
*/
//-------------------------------------------------------------------------------------------------
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
#include <windows.h>
#include <process.h>
//-------------------------------------------------------------------------------------------------
#if MSDN
::SetEvent - set signaled state
::ResetEvent - set nonsignaled state
#endif
#if 0
const BOOL g_cbManualReset = FALSE;
const BOOL g_cbIsSignaled = FALSE;
/*
Thread A: start
Thread A: finished
Thread B: start
Thread B: finished
*/
#endif
#if 0
const BOOL g_cbManualReset = FALSE;
const BOOL g_cbIsSignaled = TRUE;
/*
Thread A: start
Thread B: start
Thread B: finished
Thread A: finished
Press any key to exit...
*/
#endif
#if 0
const BOOL g_cbManualReset = TRUE;
const BOOL g_cbIsSignaled = FALSE;
/*
Thread A: start
Thread A: finished
Thread B: start
Thread B: ResetEvent
Thread B: finished
*/
#endif
#if 1
const BOOL g_cbManualReset = TRUE;
const BOOL g_cbIsSignaled = TRUE;
/*
Thread A: start
Thread B: start
Thread A: finished
Thread B: ResetEvent
Thread B: finished
*/
#endif
HANDLE g_evEvent {};
//-------------------------------------------------------------------------------------------------
unsigned int __stdcall
threadA(void *param)
{
std::cout << "Thread A: start" << std::endl;
BOOL blRv = ::SetEvent(g_evEvent);
assert(FALSE != blRv);
std::cout << "Thread A: finished" << std::endl;
return 0U;
}
//-------------------------------------------------------------------------------------------------
unsigned int __stdcall
threadB(void *param)
{
::WaitForSingleObject(g_evEvent, INFINITE);
std::cout << "Thread B: start" << std::endl;
if (g_cbManualReset == TRUE) {
BOOL blRv = ::ResetEvent(g_evEvent);
assert(FALSE != blRv);
std::cout << "Thread B: ResetEvent" << std::endl;
}
std::cout << "Thread B: finished" << std::endl;
return 0U;
}
//-------------------------------------------------------------------------------------------------
unsigned int __stdcall
threadC(void *param)
{
::WaitForSingleObject(g_evEvent, INFINITE);
std::cout << "Thread C: start" << std::endl;
if (g_cbManualReset == TRUE) {
BOOL blRv = ::ResetEvent(g_evEvent);
assert(FALSE != blRv);
std::cout << "Thread C: ResetEvent" << std::endl;
}
std::cout << "Thread C: finished" << std::endl;
return 0U;
}
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
g_evEvent = ::CreateEvent(nullptr, g_cbManualReset, g_cbIsSignaled, nullptr);
assert(g_evEvent != nullptr);
HANDLE handleA = (HANDLE)::_beginthreadex(nullptr, 0U, &threadA, nullptr, 0U, nullptr);
HANDLE handleB = (HANDLE)::_beginthreadex(nullptr, 0U, &threadB, nullptr, 0U, nullptr);
// HANDLE handleC = (HANDLE)::_beginthreadex(nullptr, 0U, &threadC, nullptr, 0U, nullptr);
::WaitForSingleObject(handleA, INFINITE);
::WaitForSingleObject(handleB, INFINITE);
// ::WaitForSingleObject(handleC, INFINITE);
::CloseHandle(handleA);
::CloseHandle(handleB);
// ::CloseHandle(handleC);
::CloseHandle(g_evEvent);
std::cout << "Press any key to exit..." << std::endl;
getchar();
return EXIT_SUCCESS;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
$ ./main
#endif