-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest20.cpp
More file actions
executable file
·47 lines (38 loc) · 788 Bytes
/
test20.cpp
File metadata and controls
executable file
·47 lines (38 loc) · 788 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
42
43
44
45
46
47
#include <iostream>
#include <cstdlib>
#include "thread.h"
#include <queue>
using std::cout;
using std::endl;
mutex m;
cv cv1;
std::queue<int> q;
void enqueue(void *a){
intptr_t arg = (intptr_t) a;
m.lock();
q.push(arg);
cout << "enqueueing " << q.front() << endl;
cv1.signal();
m.unlock();
}
void dequeue(void *a){
m.lock();
while (q.empty()){
cv1.wait(m);
}
cout << "dequeueing " << q.front() << endl;
q.pop();
m.unlock();
}
void parent(void *arg){
m.lock();
cout << "PARENT running\n";
thread th1(dequeue, (void *) 0);
thread th2(enqueue, (void *) 1);
thread::yield();
cout << "PARENT finishing\n";
m.unlock();
}
int main(){
cpu::boot(1, (thread_startfunc_t) parent, (void *) 10, false, false, 0);
}