-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest16.cpp
More file actions
executable file
·95 lines (71 loc) · 1.64 KB
/
test16.cpp
File metadata and controls
executable file
·95 lines (71 loc) · 1.64 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdlib>
#include "thread.h"
using std::cout;
using std::endl;
mutex mlock;
mutex printlock;
void child1(void *arg);
void child2(void *arg);
void child3(void *arg);
void parent(void *arg);
void child1(void *arg){
printlock.lock();
cout << (char*)arg << " running\n";
printlock.unlock();
mlock.lock();
thread th3(child2, (void *) "th3");
printlock.lock();
cout << (char*)arg << " unlocking\n";
printlock.unlock();
mlock.unlock();
printlock.lock();
cout << (char*)arg << " finishes\n";
printlock.unlock();
}
void child2(void *arg){
printlock.lock();
cout << (char*)arg << " running\n";
printlock.unlock();
thread th4(child3, (void *) "th4");
thread::yield();
printlock.lock();
cout << (char*)arg << " finishes\n";
printlock.unlock();
}
void child3(void *arg){
printlock.lock();
cout << (char*)arg << " running\n";
printlock.unlock();
mlock.lock();
printlock.lock();
cout << (char*)arg << " unlocking\n";
printlock.unlock();
mlock.unlock();
printlock.lock();
cout << (char*)arg << " finishes\n";
printlock.unlock();
}
void parent(void *arg){
printlock.lock();
cout << (char*)arg << " running\n";
printlock.unlock();
mlock.lock();
thread th1(child1, (void *) "th1");
thread th2(child2, (void *) "th2");
printlock.lock();
cout << (char*)arg << " yielding\n";
printlock.unlock();
thread::yield();
printlock.lock();
cout << (char*)arg << " unlocking\n";
printlock.unlock();
mlock.unlock();
thread::yield();
printlock.lock();
cout << (char*)arg << " finish\n";
printlock.unlock();
}
int main(){
cpu::boot(1, (thread_startfunc_t) parent, (void *) "parent", false, false, 1);
}