forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatomic.cpp
More file actions
37 lines (29 loc) · 840 Bytes
/
atomic.cpp
File metadata and controls
37 lines (29 loc) · 840 Bytes
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
/*
# atomic
More restricted than mutex as it can only protect a few operations on integers.
But if that is the use case, may be more efficient.
On GCC 4.8 x86-64, using atomic is a huge peformance improvement
over the same program with mutexes (5x).
*/
#include "common.hpp"
#if __cplusplus >= 201103L
std::atomic_long global(0);
void threadMain() {
for (int i = 0; i < NUM_ITERS; ++i) {
global++;
}
}
#endif
int main() {
#if __cplusplus >= 201103L
std::thread threads[NUM_THREADS];
int i;
for (i = 0; i < NUM_THREADS; ++i)
threads[i] = std::thread(threadMain);
for (i = 0; i < NUM_THREADS; ++i)
threads[i].join();
assert(global.load() == NUM_THREADS * NUM_ITERS);
// Same as above through `operator T`.
assert(global == NUM_THREADS * NUM_ITERS);
#endif
}