-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCase-ThreadYield.cpp
More file actions
88 lines (68 loc) · 2.13 KB
/
TestCase-ThreadYield.cpp
File metadata and controls
88 lines (68 loc) · 2.13 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
#include <iostream>
#include "uthread.h"
using namespace std;
void* t1(void *i){
cout << "Starting Thread 1" << endl;
int *arg = (int *)i;
for ( int i = 0 ; i < 3 ; i++ ){
for(int j = 0; j < 3; j++) {
cout << "Argument passed in T1 : " << *arg << endl;
}
cout << "yielding from T1" << endl;
uthread_yield();
}
cout << "Ending Thread 1" << endl;
return (void*)new string("output of t1");
}
void* t2(void *i){
cout << "Starting Thread 2" << endl;
int *arg = (int *)i;
for ( int i = 0 ; i < 3 ; i++ ){
for(int j = 0; j < 3; j++) {
cout << "Argument passed in T2 : " << *arg << endl;
}
cout << "yielding from T2" << endl;
uthread_yield();
}
cout << "Ending Thread 2" << endl;
return (void*)new string("output of t2");
}
void* t3(void *i){
cout << "Starting Thread 3" << endl;
int *arg = (int *)i;
for ( int i = 0 ; i < 3 ; i++ ){
for(int j = 0; j < 3; j++) {
cout << "Argument passed in T3 : " << *arg << endl;
}
cout << "yielding from T3" << endl;
uthread_yield();
}
cout << "Ending Thread 3" << endl;
return (void*)new string("output of t3");
}
int main() {
// Put Main TCB in sch_queue
int t1Arg = 1;
int t1PID;
void *ar1 = (void*)malloc(100);
void **t1RetVal = (void**)&ar1;
t1PID = uthread_create(t1, (void*)(&t1Arg));
int t2Arg = 2;
int t2PID;
void *ar2 = (void*)malloc(100);
void **t2RetVal = (void**)&ar2;
t2PID = uthread_create(t2, (void*)(&t2Arg));
int t3Arg = 3;
int t3PID;
void *ar3 = (void*)malloc(100);
void **t3RetVal = (void**)&ar3;
t3PID = uthread_create(t3, (void*)(&t3Arg));
uthread_join(t1PID,t1RetVal);
cout << "t1 joined and the returned value is : "<< *(string *)*t1RetVal << endl;
uthread_join(t2PID,t2RetVal);
cout << "t2 joined and the returned value is : "<< *(string *)*t2RetVal << endl;
uthread_join(t3PID,t3RetVal);
cout << "t3 joined and the returned value is : "<< *(string *)*t3RetVal << endl;
cout << "Main done" << endl;
return 0;
}