forked from neosmart/pevents
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample.cpp
77 lines (67 loc) · 1.23 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
#include "pevents.h"
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <assert.h>
using namespace neosmart;
using namespace std;
void *numbers(void *param);
void *letters(void *param);
neosmart_event_t event[2];
char letter = '?';
int number = -1;
char lastChar = 'Z';
int lastNum = 100;
void *letters(void *param)
{
for(int i = 0; ; ++i)
{
letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[i%26];
SetEvent(event[0]);
sleep(3);
}
}
void *numbers(void *param)
{
for(int i = 0; ; ++i)
{
number = i;
SetEvent(event[1]);
sleep(4);
}
}
int main()
{
event[0] = CreateEvent();
event[1] = CreateEvent();
pthread_t thread1, thread2;
pthread_create(&thread1, NULL, letters, NULL);
pthread_create(&thread2, NULL, numbers, NULL);
for(int i = 0; i < 25; ++i)
{
int index = 7;
if(WaitForMultipleEvents(event, 2, false, -1, index) != 0)
{
cout << "Timeout!" << 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! Index: " << index << endl;
exit(-1);
}
}
DestroyEvent(event[0]);
DestroyEvent(event[1]);
}