-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
64 lines (45 loc) · 1.81 KB
/
main.cpp
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
//standard includes
#include <iostream>
//3rd party library includes
//my includes
#include "CThread_Add.h"
//any "using" statements
using namespace std;
//any global variables
int main(int argc, char* argv[]) {
CThread_Add* CThread_Add_ptr;
CThread_Add_ptr = new CThread_Add;
/*
cout << "\n---------------------------------------\n" << endl;
cout << "There are " << CThread_Add_ptr->threadCount << " threads." << endl;
cout << "\n---------------------------------------\n" << endl;
*/
int* threadInt; //thread will operate on this
int* mainInt; // main() will operate on this
threadInt = new int;
mainInt = new int;
*threadInt = 0; // initial value before threads act
*mainInt = 0;
cout << "\n---------------------------------------\n" << endl;
cout << "Thread's initial value is = " << *threadInt << endl;
cout << "main() initial value is = " << *mainInt << endl;
cout << "\n---------------------------------------\n" << endl;
CThread_Add_ptr->start(threadInt,sizeof(int));
usleep(1000);
for (int i = 0; i < 10; i++) {
*mainInt += 1;
cout << "\nmain() current value is = " << *mainInt << endl;
usleep(100000); // the ratio of this number and the one in CThread_Add->execute() determine how many times the thread runs vs. this for-loop in main
CThread_Add_ptr->update(threadInt);
cout << "Thread's current value is = " << *threadInt << endl;
}
CThread_Add_ptr->stop();
CThread_Add_ptr->update(threadInt);
usleep(100000); // to make sure the thread's final value has enough time to compute
cout << "\n---------------------------------------\n" << endl;
cout << "Thread's final value is = " << *threadInt << endl;
cout << "main() final value is = " << *mainInt << endl;
cout << "\n---------------------------------------\n" << endl;
delete CThread_Add_ptr;
return 0;
}