-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest14.cpp
More file actions
executable file
·61 lines (50 loc) · 1.38 KB
/
test14.cpp
File metadata and controls
executable file
·61 lines (50 loc) · 1.38 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
// circular join
#include <iostream>
#include <cstdlib>
#include "thread.h"
using std::cout;
using std::endl;
mutex mutex1;
cv cv1;
void child(void *a) {
mutex1.lock();
cout << "child acquire lock" << endl;
try {
mutex1.unlock();
cout << "child successfully unlock" << endl;
}
catch (std::runtime_error&) {
cout << "error: child has lock, but cannot successfully unlock" << endl;
}
try {
mutex1.unlock();
cout << "child try to acquire lock that it does not own" << endl;
}
catch (std::runtime_error&) {
cout << "catch error: child try to unlock a lock it does not own" << endl;
}
try {
cv1.wait(mutex1);
cout << "child try to wait on cv (but does not have lock)" << endl;
}
catch (std::runtime_error&) {
cout << "catch error: child try to wait on cv (but does not have lock)" << endl;
}
cout << "child 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) child, (void *) "child");
cout << "constructed th1" << endl;
mutex1.lock();
thread::yield();
mutex1.unlock();
cout << "parent finishes" << endl;
}
int main()
{
cpu::boot(1, (thread_startfunc_t) parent, (void *)100, false, false, 1);
}