-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest13.cpp
More file actions
executable file
·81 lines (69 loc) · 1.87 KB
/
test13.cpp
File metadata and controls
executable file
·81 lines (69 loc) · 1.87 KB
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
// test broadcast
#include <iostream>
#include <cstdlib>
#include "thread.h"
using std::cout;
using std::endl;
mutex mutex1;
mutex mutex2;
cv cv1;
int count = 2;
void child1(void *a) {
mutex1.lock();
char *message = (char *) a;
cout << "child 1 called with message " << message << endl;
while (count--) {
cout << "count > 0, child1 wait" << endl;
cv1.wait(mutex1);
}
count = 5;
cout << "child 1 wake up, set count=5" << endl;
cv1.broadcast();
cout << "child1 broadcast" << endl;
cout << "child1 finishes" << endl;
mutex1.unlock();
}
void child2(void *a) {
mutex1.lock();
char *message = (char *) a;
cout << "child 2 called with message " << message << endl;
while (count--) {
cout << "count > 0, child2 wait" << endl;
cv1.wait(mutex1);
}
count = 0;
cout << "child 2 wake up, set count=0" << endl;
cv1.broadcast();
cout << "child2 broadcast" << endl;
cout << "child2 waits" << endl;
mutex2.lock();
cout << "child2 mutex2.lock()" << endl;
mutex1.unlock();
cv1.wait(mutex2);
cout << "child2 wakes up" << endl;
cout << "child2 finishes" << endl;
}
void parent(void *a) {
intptr_t arg = (intptr_t) a;
mutex1.lock();
cout << "parent called with arg " << arg << endl;
mutex1.unlock();
thread t1 ((thread_startfunc_t) child1, (void *) "CHILD 1");
thread t2 ((thread_startfunc_t) child2, (void *) "CHILD 2");
mutex1.lock();
cout << "mutex1 locked" << endl;
while (count--) {
cout << "count > 0, parent wait" << endl;
cv1.wait(mutex1);
}
count = 3;
cout << "parent wake up, set count=3" << endl;
cv1.broadcast();
cout << "parent broadcast" << endl;
cout << "parent finishes" << endl;
mutex1.unlock();
}
int main()
{
cpu::boot(1, (thread_startfunc_t) parent, (void *)100, false, false, 1);
}