-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
58 lines (51 loc) · 1.64 KB
/
main.cpp
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
#include "mcs_lock.hpp"
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <mutex>
#include <thread>
#include <vector>
template <typename Lock> void counter(Lock &l, uint64_t &cnt, uint64_t num) {
for (uint64_t i = 0; i < num; ++i) {
l.lock();
cnt++;
l.unlock();
}
}
template <typename Lock>
void test_lock(size_t n_threads, uint64_t cnt_per_thread) {
alignas(64) Lock m;
alignas(64) uint64_t cnt = 0;
std::vector<std::thread> threads;
threads.reserve(n_threads);
for (int i = 0; i < n_threads; i++) {
threads.emplace_back(counter<Lock>, std::ref(m), std::ref(cnt),
cnt_per_thread);
}
for (int i = 0; i < n_threads; i++) {
threads[i].join();
}
assert(cnt == n_threads * cnt_per_thread);
}
int main(int argc, const char *argv[]) {
if (argc != 3) {
printf("num_threads counts_per_thread");
exit(1);
}
size_t n = static_cast<size_t>(std::stoi(argv[1], nullptr, 10));
uint64_t cnt_per_thread =
static_cast<size_t>(std::stoi(argv[2], nullptr, 10));
printf("Counting %lu x %lu with %lu threads\n", n, cnt_per_thread, n);
auto t1 = std::chrono::high_resolution_clock::now();
test_lock<std::mutex>(n, cnt_per_thread);
auto t2 = std::chrono::high_resolution_clock::now();
test_lock<MCSLock>(n, cnt_per_thread);
auto t3 = std::chrono::high_resolution_clock::now();
printf("Time taken (milliseconds):\n");
printf(
" %-11s : %lu\n", "std::mutex",
std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count());
printf(
" %-11s : %lu\n", "MCSLock",
std::chrono::duration_cast<std::chrono::milliseconds>(t3 - t2).count());
}