Repository navigation
Expand file tree
/
Copy paththread.cpp
More file actions
85 lines (65 loc) · 2.05 KB
/
Copy paththread.cpp
File metadata and controls
85 lines (65 loc) · 2.05 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
/*
TODO this example is too complex, split it up into smaller examples.
# Thread
# Multithreading
# Parallel
# Concurrency
Requires `-pthread` flag on g++ Linux.
*/
#include "common.hpp"
int nNsecs = 10;
int threadGlobal = 0;
int threadGlobalMutexed = 0;
std::mutex threadGlobalMutex;
std::thread::id lastThreadId;
std::set<std::thread::id> threadIds;
int threadGlobalEq0 = 0;
int threadGlobalMutexedEq0 = 0;
int threadChange = 0;
void threadMain(int threadCountToSqrt) {
std::thread::id id = std::this_thread::get_id();
for (int i = 0; i < threadCountToSqrt; i++) {
for (int j = 0; j<threadCountToSqrt; j++) {
if (lastThreadId != id) {
++threadChange;
//threadIds.insert(id);
lastThreadId = id;
}
// cout is not thread safe order gets mixed up.
//std::cout << id << " " << i << endl;
// If happens.
threadGlobal = 1;
if (threadGlobal == 0)
++threadGlobalEq0;
threadGlobal = 0;
// If never happens!
threadGlobalMutex.lock();
// if not available, wait
//threadGlobalMutex.try_lock();
// If not available, return!
threadGlobalMutexed = 1;
if (threadGlobalMutexed == 0)
++threadGlobalMutexedEq0;
threadGlobalMutexed = 0;
threadGlobalMutex.unlock();
}
}
std::this_thread::sleep_for (std::chrono::nanoseconds(nNsecs));
// Done, pass to another thread.
std::this_thread::yield();
}
int main() {
// Start threads.
std::thread t1(threadMain, 1000);
std::thread t2(threadMain, 1000);
// Ensure that both threads ended.
t1.join();
t2.join();
assert(threadChange > 0);
//assert(threadIds.size() == 2);
//assert(threadGlobalEq0 > 0);
assert(threadGlobalMutexedEq0 == 0);
std::thread::id mainId = std::this_thread::get_id();
std::this_thread::sleep_for(std::chrono::nanoseconds(nNsecs));
std::this_thread::yield();
}