-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarrierTester.cpp
More file actions
122 lines (92 loc) · 2.22 KB
/
Copy pathbarrierTester.cpp
File metadata and controls
122 lines (92 loc) · 2.22 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* \file barrierTester.cpp
* \brief Source file for a Barrier class' test
* \author Luca Di Mauro
*/
#include <barrier.hpp>
#include <iostream>
#include <thread>
using namespace std;
int main (int argn, char **argv) {
cout << "Hello user!\n";
{
// TEST 1
Barrier b (3);
thread t0 = thread ([&b] () {
b.decreaseBarrier ();
cout << "Unlocked t0!\n";
});
thread t1 = thread ([&b] () {
b.decreaseBarrier ();
cout << "Unlocked t1!\n";
});
cout << "Sleeping...\n";
std::this_thread::sleep_for (chrono::milliseconds (3000));
cout << "Decreasing..\n";
b.decreaseBarrier ();
cout << "Not locked!\n";
t0.join ();
t1.join ();
cout << endl << "===== TEST 1 PASSED =====\n\n\n";
}
{
// TETST 2
Barrier b (3);
try {
cout << "Sleeping...\n";
std::this_thread::sleep_for (chrono::milliseconds (3000));
cout << "Resetting..\n";
b.reset (3);
} catch (std::runtime_error &e) {
cout << "Catched an error: " << e.what () << endl;
}
cout << endl << "===== TEST 2 PASSED =====\n\n\n";
}
{
// TEST 3
Barrier b (3);
thread t00 = thread ([&b] () {
b.decreaseBarrier ();
cout << "Unlocked t00!\n";
});
thread t10 = thread ([&b] () {
b.decreaseBarrier ();
cout << "Unlocked t10!\n";
});
try {
cout << "Sleeping...\n";
std::this_thread::sleep_for (chrono::milliseconds (3000));
cout << "Resetting..\n";
b.reset (3);
} catch (std::runtime_error &e) {
cout << "Catched an error: " << e.what () << endl;
}
cout << "Decreasing...\n";
b.decreaseBarrier ();
cout << "Not locked!\n";
cout << "Resetting..\n";
b.reset (3);
t00.join ();
t10.join ();
thread t01 = thread ([&b] () {
std::this_thread::sleep_for (chrono::milliseconds (1000));
b.decreaseBarrier ();
cout << "Unlocked t01!\n";
});
thread t11 = thread ([&b] () {
std::this_thread::sleep_for (chrono::milliseconds (500));
b.decreaseBarrier ();
cout << "Unlocked t11!\n";
});
thread t31 = thread ([&b] () {
std::this_thread::sleep_for (chrono::milliseconds (750));
b.decreaseBarrier ();
cout << "Unlocked t31!\n";
});
t01.join ();
t11.join ();
t31.join ();
cout << endl << "===== TEST 3 PASSED =====\n\n\n";
}
return 0;
}