forked from neosmart/pevents
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.cpp
104 lines (90 loc) · 2.18 KB
/
sample.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
/*
* WIN32 Events for POSIX
* Author: Mahmoud Al-Qudsi <[email protected]>
* Copyright (C) 2011 - 2015 by NeoSmart Technologies
* This code is released under the terms of the MIT License
*/
#include <iostream>
#include <assert.h>
#include <thread>
#include <chrono>
//On Windows, you must include Winbase.h/Synchapi.h/Windows.h before pevents.h
#ifdef _WIN32
#include <Windows.h>
#endif
#include "pevents.h"
using namespace neosmart;
using namespace std;
neosmart_event_t events[3]; //letters, numbers, abort
char letter = '?';
int number = -1;
char lastChar = '\0';
int lastNum = -1;
void letters()
{
for (uint32_t i = 0; WaitForEvent(events[2], 0) == WAIT_TIMEOUT; ++i)
{
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%26];
SetEvent(events[0]);
this_thread::sleep_for(chrono::seconds(1));
}
}
void numbers()
{
for (uint32_t i = 0; WaitForEvent(events[2], 0) == WAIT_TIMEOUT; ++i)
{
number = i;
SetEvent(events[1]);
this_thread::sleep_for(chrono::seconds(4));
}
}
int main()
{
events[0] = CreateEvent(); //letters auto-reset event
events[1] = CreateEvent(); //numbers auto-reset event
events[2] = CreateEvent(true); //abort manual-reset event
std::thread thread1(letters);
std::thread thread2(numbers);
for (uint32_t i = 0; lastChar != 'Z'; ++i)
{
int index = -1;
int result = WaitForMultipleEvents(events, 2, false, -1, index);
if (result == WAIT_TIMEOUT)
{
cout << "Timeout!" << endl;
}
else if (result != 0)
{
cout << "Error in wait!" << endl;
}
else if (index == 0)
{
assert(lastChar != letter);
cout << letter << endl;
lastChar = letter;
}
else if (index == 1)
{
assert(lastNum != number);
cout << number << endl;
lastNum = number;
}
else
{
cout << "ERROR! Unexpected index: " << index << endl;
exit(-1);
}
}
//You can't just DestroyEvent() and exit - it'll segfault
//That's because letters() and numbers() will call SetEvent on a destroyed event
//You must *never* call SetEvent/ResetEvent on a destroyed event!
//So we set an abort event and wait for the helper threads to exit
//Set the abort
SetEvent(events[2]);
thread1.join();
thread2.join();
for (auto event : events)
{
DestroyEvent(event);
}
}