-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1114_PrintInOrder.cpp
More file actions
41 lines (36 loc) · 840 Bytes
/
Copy path1114_PrintInOrder.cpp
File metadata and controls
41 lines (36 loc) · 840 Bytes
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
#include <semaphore.h>
class Foo
{
private:
sem_t sem1;
sem_t sem2;
sem_t sem3;
public:
Foo()
{
sem_init(&sem1, 0, 1);
sem_init(&sem2, 0, 0);
sem_init(&sem3, 0, 0);
}
void first(function<void()> printFirst)
{
sem_wait(&sem1);
// printFirst() outputs "first". Do not change or remove this line.
printFirst();
sem_post(&sem2);
}
void second(function<void()> printSecond)
{
sem_wait(&sem2);
// printSecond() outputs "second". Do not change or remove this line.
printSecond();
sem_post(&sem3);
}
void third(function<void()> printThird)
{
sem_wait(&sem3);
// printThird() outputs "third". Do not change or remove this line.
printThird();
sem_post(&sem1);
}
};