-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
82 lines (61 loc) · 2.92 KB
/
Copy pathmain.cpp
File metadata and controls
82 lines (61 loc) · 2.92 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
#include "../source/include/LockingSharedObject.hpp"
#include "../source/include/LoggingSharedObject.hpp"
#include "../source/include/NotCopyable.hpp"
#include "../source/include/SharedObject.hpp"
#include <thread>
using namespace std;
void access_thread_one(LockingSharedObject<int>& obj){
SharedObject<int>
::Accessor acc(obj);// Create an accessor of the LockingSharedObject
cout <<"AccesThreadOne: "<< acc.access() << endl;//Access the Accessor
}
void access_thread_two(LockingSharedObject<int> & obj){
SharedObject<int>
::Accessor acc(obj); // Create an accessor of the LockingSharedObject
cout << "AccesThreadTwo: "<< acc.access() << endl;//Access the Accessor
}
void access_thread_three(LoggingSharedObject<int>& obj){
SharedObject<int>
::Accessor acc(obj); // Create an accessor of the LockingSharedObject
cout <<"AccesThreadThree: "<< acc.access() << endl;//Access the Accessor
}
void access_thread_four(LoggingSharedObject<int> & obj){
SharedObject<int>
::Accessor acc(obj);//Create an accessor of the LockingSharedObject
cout << "AccesThreadFour: "<< acc.access() << endl;//Access the Accessor
}
int main(){
// You create two threads and give them a LockingSharedObject reference,
// each thread creates its own accessor and tries to access it.
// Because the Accessor constructor claims the accessor when you create it,
// after one of the threads creates the Accessor,
// the other thread has to wait for the first thread to finish because the
// Accessor is only released when the destructor is called.
//************ LockingSharedObject ************//
cout << "************ LockingSharedObject ************" << endl;
int a = 1; // create an int with a value
LockingSharedObject<int>
example_a(a);//assign the int to the LockingSharedObject
thread first_a(access_thread_one,
std::ref(example_a));// Create a thread and give it a
//std::ref of the LockingSharedObject
thread second_a(access_thread_two,
std::ref(example_a));// Create a thread and give it a
//std::ref of the LockingSharedObject
first_a.join(); // Wait for thread 1 to finish
second_a.join(); // Wait for thread 2 to finish
//************ LoggingSharedObject ************//
cout << "************ LoggingSharedObject ************" << endl;
int b = 2; // create an int with a value
LoggingSharedObject<int>
example_b(b);//assign the int to the LockingSharedObject
thread first_b(access_thread_three,
std::ref(example_b));// Create a thread and give it a
//std::ref of the LockingSharedObject
thread second_b(access_thread_four,
std::ref(example_b));// Create a thread and give it a
//std::ref of the LockingSharedObject
first_b.join(); // Wait for thread 1 to finish
second_b.join(); // Wait for thread 2 to finish
return 0;
}