-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcas.cpp
32 lines (26 loc) · 793 Bytes
/
cas.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
#include <benchmark/benchmark.h>
#include <mutex>
#include <atomic>
#include <chrono>
#include <thread>
namespace cas
{
std::atomic_long x(0);
static void BM_CASAdd(benchmark::State& state) {
if (state.thread_index == 0) {
x = 0;
}
while (state.KeepRunning()) {
long read_x = x.load(std::memory_order_relaxed);
while (!x.compare_exchange_strong(read_x, read_x + 1, std::memory_order_relaxed)) {}
benchmark::DoNotOptimize(x);
}
benchmark::ClobberMemory();
}
#define ARGS(N) ->Threads(N)->UseRealTime();
// Register the function as a benchmark
BENCHMARK(BM_CASAdd) ARGS(1);
BENCHMARK(BM_CASAdd) ARGS(2);
BENCHMARK(BM_CASAdd) ARGS(4);
BENCHMARK(BM_CASAdd) ARGS(8);
}