forked from double-free/tiny-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheffi_cmp.cpp
More file actions
118 lines (104 loc) · 3.14 KB
/
Copy patheffi_cmp.cpp
File metadata and controls
118 lines (104 loc) · 3.14 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
/************************************
* Compare between mutex and atomic *
************************************/
#include <atomic>
#include <chrono>
#include <cstddef>
#include <iostream>
#include <mutex>
#include <thread>
// 需要能整除
const int COUNTS = 100000;
const int THREAD_NUM = 2;
void unsafe_add() {
size_t total = 0;
std::thread threads[THREAD_NUM];
int per_thread_count = COUNTS / THREAD_NUM;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = std::thread([&]() {
for (int j = 0; j < per_thread_count; j++) {
total += 1;
}
});
}
for (int i = 0; i < THREAD_NUM; i++) {
threads[i].join();
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "unsafe add: result = " << total << ", time = " << diff.count()
<< " seconds" << std::endl;
}
void safe_add() {
size_t total = 0;
std::thread threads[THREAD_NUM];
int per_thread_count = COUNTS / THREAD_NUM;
std::mutex mu;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = std::thread([&]() {
for (int j = 0; j < per_thread_count; j++) {
std::lock_guard<std::mutex> lg(mu);
total += 1;
}
});
}
for (int i = 0; i < THREAD_NUM; i++) {
threads[i].join();
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "mutex add: result = " << total << ", time = " << diff.count()
<< " seconds" << std::endl;
}
void atomic_add() {
std::atomic<size_t> total(0);
std::thread threads[THREAD_NUM];
int per_thread_count = COUNTS / THREAD_NUM;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = std::thread([&]() {
for (int j = 0; j < per_thread_count; j++) {
total.fetch_add(1, std::memory_order_relaxed);
}
});
}
for (int i = 0; i < THREAD_NUM; i++) {
threads[i].join();
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "atomic add: result = " << total << ", time = " << diff.count()
<< " seconds" << std::endl;
}
void spinlock_add() {
size_t total = 0;
std::thread threads[THREAD_NUM];
int per_thread_count = COUNTS / THREAD_NUM;
std::atomic_flag spinlock = ATOMIC_FLAG_INIT;
auto start = std::chrono::steady_clock::now();
for (int i = 0; i < THREAD_NUM; i++) {
threads[i] = std::thread([&]() {
for (int j = 0; j < per_thread_count; j++) {
while (spinlock.test_and_set(std::memory_order_acquire)) {
}
total += 1;
spinlock.clear(std::memory_order_release);
}
});
}
for (int i = 0; i < THREAD_NUM; i++) {
threads[i].join();
}
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> diff = end - start;
std::cout << "spinlock add: result = " << total << ", time = " << diff.count()
<< " seconds" << std::endl;
}
int main(int argc, const char *argv[]) {
unsafe_add();
safe_add();
atomic_add();
spinlock_add();
}