-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest9.cpp
More file actions
executable file
·81 lines (67 loc) · 1.84 KB
/
test9.cpp
File metadata and controls
executable file
·81 lines (67 loc) · 1.84 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 switch thread
//cpu_init()
//thread::yield()
//mutex::lock()
//cv::wait()
#include <iostream>
#include "thread.h"
using std::cout;
using std::endl;
mutex mutex1;
mutex mutex2;
cv cv1;
int child_done = 0; // global variable; shared between the two threads
void grandchild(void *a)
{
char *message = (char *) a;
mutex2.lock();
cout << "running grandchild " << message << endl;
cout << "grandchild exiting " << message << endl;
mutex2.unlock();
}
void child(void *a)
{
char *message = (char *) a;
mutex1.lock();
cout << "child called with message " << message << endl;
thread t2 ((thread_startfunc_t) grandchild, (void *) "grandchild2 message");
thread t3 ((thread_startfunc_t) grandchild, (void *) "grandchild3 message");
t2.join();
cout << "finish join grandchild 2" << endl;
t3.join();
cout << "finish join grandchild 3" << endl;
child_done = 1;
cout << child_done << endl;
cv1.signal();
cout << "cv1 signal" << endl;
mutex1.unlock();
}
void parent(void *a)
{
intptr_t arg = (intptr_t) a;
mutex1.lock();
cout << "parent called with arg " << arg << endl;
mutex1.unlock();
//yield shouldn't do anything here
thread::yield();
cout << "yield didn't do anything"<<endl;
//create child thread
thread t1 ((thread_startfunc_t) child, (void *) "test message");
cout << "t1 created " << endl;
mutex1.lock();
//should start running child
cout << "yield to child"<<endl;
thread::yield();
cout << "came back to parent" <<endl;
while (!child_done) {
cout << "parent waiting for child to run\n";
cv1.wait(mutex1);
}
cout << "parent finishing" << endl;
mutex1.unlock();
cout << "mutex1 unlocked" << endl;
}
int main()
{
cpu::boot(1, (thread_startfunc_t) parent, (void *)100, false, false, 0);
}