forked from gpertea/gclib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.cpp
More file actions
283 lines (247 loc) · 6.88 KB
/
threads.cpp
File metadata and controls
283 lines (247 loc) · 6.88 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <iostream>
#include <list>
#include "GThreads.h"
using namespace std;
//using namespace tthread;
// Thread local storage variable (not available on Mac OSX ?!)
#ifndef GTHREADS_NO_TLS
thread_local int gLocalVar;
#endif
// Mutex + global count variable
GMutex gMutex;
GFastMutex gFastMutex;
int gCount;
// Condition variable
GConditionVar gCond;
// Thread function: Thread ID
void ThreadIDs(GThreadData& my)
{
{
GLockGuard<GMutex> lock(gMutex);
//cout << " My thread id is " << thisThread->get_id() << "." << endl << flush;
cout << " My thread id is " << my.thread->get_id() << "." << endl << flush;
}
this_thread::sleep_for(chrono::seconds(4));
}
#ifndef GTHREADS_NO_TLS
// Thread function: Thread-local storage
void ThreadTLS(void * aArg)
{
gLocalVar = 2;
cout << " My gLocalVar is " << gLocalVar << "." << endl;
}
#endif
// Thread function: Mutex locking
void ThreadLock(void * aArg)
{
for(int i = 0; i < 10000; ++ i)
{
GLockGuard<GMutex> lock(gMutex);
++ gCount;
}
}
// Thread function: Mutex locking
void ThreadLock2(void * aArg)
{
for(int i = 0; i < 10000; ++ i)
{
GLockGuard<GFastMutex> lock(gFastMutex);
++ gCount;
}
}
// Thread function: Condition notifier
void ThreadCondition1(void * aArg)
{
GLockGuard<GMutex> lock(gMutex);
-- gCount;
gCond.notify_all();
}
// Thread function: Condition waiter
void ThreadCondition2(void * aArg)
{
cout << " Wating:" << flush;
GLockGuard<GMutex> lock(gMutex);
while(gCount > 0)
{
cout << "*" << gCount << flush;
gCond.wait(gMutex);
cout << "received notification at " << gCount << endl << flush;
}
cout << "-reached 0!" << endl;
}
// Thread function: Yield
void ThreadYield(void * aArg)
{
// Yield...
this_thread::yield();
}
// This is the main program (i.e. the main thread)
int main()
{
// Test 1: Show number of CPU cores in the system
cout << "PART I: Info" << endl;
cout << " Number of processor cores: " << GThread::hardware_concurrency() << endl;
// Test 2: thread IDs
/*
cout << endl << "PART II: Thread IDs" << endl;
{
// Show the main thread ID
//cout << " Main thread id is " << this_thread::get_id() << "." << endl;
// Start a bunch of child threads - only run a single thread at a time
GThread t1(ThreadIDs, 0);
this_thread::sleep_for(chrono::milliseconds(400));
GThread t2(ThreadIDs, 0);
this_thread::sleep_for(chrono::milliseconds(400));
GThread t3(ThreadIDs, 0);
this_thread::sleep_for(chrono::milliseconds(400));
GThread t4(ThreadIDs, 0);
//t1.join();
//t2.join();
//t3.join();
//t4.join();
this_thread::sleep_for(chrono::milliseconds(200));
cout << "Waiting for all threads to finish.." << flush;
int num_threads;
while ((num_threads=GThread::num_running())>0) {
cout << "." << num_threads << "." << flush;
this_thread::sleep_for(chrono::milliseconds(300));
}
cout << ".. Done. " << endl;
}
// Test 3: thread local storage
cout << endl << "PART III: Thread local storage" << endl;
#ifndef GTHREADS_NO_TLS
{
// Clear the TLS variable (it should keep this value after all threads are
// finished).
gLocalVar = 1;
cout << " Main gLocalVar is " << gLocalVar << "." << endl;
// Start a child thread that modifies gLocalVar
GThread t1(ThreadTLS, 0);
t1.join();
// Check if the TLS variable has changed
if(gLocalVar == 1)
cout << " Main gLocalVar was not changed by the child thread - OK!" << endl;
else
cout << " Main gLocalVar was changed by the child thread - FAIL!" << endl;
}
#else
cout << " TLS is not supported on this platform..." << endl;
#endif
// Test 4: mutex locking
cout << endl << "PART IV: Mutex locking (100 threads x 10000 iterations)" << endl;
{
// Clear the global counter.
gCount = 0;
// Start a bunch of child threads
list<GThread *> threadList;
for(int i = 0; i < 100; ++ i)
threadList.push_back(new GThread(ThreadLock, 0));
// Wait for the threads to finish
list<GThread *>::iterator it;
for(it = threadList.begin(); it != threadList.end(); ++ it)
{
GThread * t = *it;
t->join();
delete t;
}
// Check the global count
cout << " gCount = " << gCount << endl;
}
// Test 5: fast_mutex locking
cout << endl << "PART V: Fast mutex locking (100 threads x 10000 iterations)" << endl;
{
// Clear the global counter.
gCount = 0;
// Start a bunch of child threads
list<GThread *> threadList;
for(int i = 0; i < 100; ++ i)
threadList.push_back(new GThread(ThreadLock2, 0));
// Wait for the threads to finish
list<GThread *>::iterator it;
for(it = threadList.begin(); it != threadList.end(); ++ it)
{
GThread * t = *it;
t->join();
delete t;
}
// Check the global count
cout << " gCount = " << gCount << endl;
}
*/
// Test 6: condition variable
cout << endl << "PART VI: Condition variable (40 + 1 threads)" << endl;
{
// Set the global counter to the number of threads to run.
{
GLockGuard<GMutex> lock(gMutex);
gCount = 6;
}
// Start the waiting thread (it will wait for gCount to reach zero).
GThread t1(ThreadCondition2, 0);
this_thread::sleep_for(chrono::milliseconds(400));
{
for (int c=5;c>=0;c--) {
{
GLockGuard<GMutex> lock(gMutex);
--gCount;
gCond.notify_one();
}
cout << "Notify_" << c << "|" << flush;
//this_thread::sleep_for(chrono::milliseconds(200));
cout << "Notify_sent" << c << "|" << flush;
}
}
// Start a bunch of child threads (these will decrease gCount by 1 when they
// finish)
/*
list<GThread *> threadList;
for(int i = 0; i < 40; ++ i)
threadList.push_back(new GThread(ThreadCondition1, 0));
*/
// Wait for the waiting thread to finish
t1.join();
// Wait for the other threads to finish
/*
list<GThread *>::iterator it;
for(it = threadList.begin(); it != threadList.end(); ++ it)
{
GThread * t = *it;
t->join();
delete t;
}
*/
}
/*
// Test 7: yield
cout << endl << "PART VII: Yield (40 + 1 threads)" << endl;
{
// Start a bunch of child threads
list<GThread *> threadList;
for(int i = 0; i < 40; ++ i)
threadList.push_back(new GThread(ThreadYield, 0));
// Yield...
this_thread::yield();
// Wait for the threads to finish
list<GThread *>::iterator it;
for(it = threadList.begin(); it != threadList.end(); ++ it)
{
GThread * t = *it;
t->join();
delete t;
}
}
// Test 8: sleep
cout << endl << "PART VIII: Sleep (10 x 100 ms)" << endl;
{
// Sleep...
cout << " Sleeping" << flush;
for(int i = 0; i < 10; ++ i)
{
this_thread::sleep_for(chrono::milliseconds(100));
cout << "." << flush;
}
cout << endl;
}
*/
}