-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcs_example.cpp
More file actions
102 lines (80 loc) · 2.36 KB
/
mcs_example.cpp
File metadata and controls
102 lines (80 loc) · 2.36 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
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include "thread/sync/mcs_spinlock.hpp"
class SharedCounter {
public:
explicit SharedCounter(int initial_value = 0) : value_(initial_value) {
}
void Increment() {
thread::sync::QueueSpinLock::Guard guard{lock_};
++value_;
}
int GetValue() const {
return value_;
}
private:
thread::sync::QueueSpinLock lock_;
int value_;
};
struct BenchmarkConfig {
int num_threads = 4;
int increments_per_thread = 100000;
};
class BenchmarkRunner {
public:
explicit BenchmarkRunner(const BenchmarkConfig& config) : config_(config) {
}
void Run() {
PrintHeader();
SharedCounter counter;
auto start = std::chrono::high_resolution_clock::now();
RunWorkers(counter);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
PrintResults(counter.GetValue(), duration.count());
}
private:
void PrintHeader() const {
std::cout << "Starting MCS Spinlock benchmark...\n";
std::cout << "Threads: " << config_.num_threads << "\n";
std::cout << "Increments per thread: " << config_.increments_per_thread << "\n\n";
}
void RunWorkers(SharedCounter& counter) {
std::vector<std::thread> threads;
for (int i = 0; i < config_.num_threads; ++i) {
threads.emplace_back([&counter, i, this]() {
for (int j = 0; j < config_.increments_per_thread; ++j) {
counter.Increment();
}
std::cout << "Thread " << i << " completed\n";
});
}
for (auto& thread : threads) {
thread.join();
}
}
void PrintResults(int actual, long long duration_ms) const {
int expected = config_.num_threads * config_.increments_per_thread;
std::cout << "\n=== Results ===\n";
std::cout << "Time taken: " << duration_ms << " ms\n";
std::cout << "Final counter value: " << actual << "\n";
std::cout << "Expected value: " << expected << "\n";
if (actual == expected) {
std::cout << "MCS Spinlock works correctly!\n";
} else {
std::cout << "MCS Spinlock failed!\n";
throw std::runtime_error("MCS Spinlock failed");
}
}
BenchmarkConfig config_;
};
int main() {
BenchmarkConfig config{
.num_threads = 4,
.increments_per_thread = 100000,
};
BenchmarkRunner runner(config);
runner.Run();
}