Skip to content

Commit 7317f26

Browse files
committed
feat: add multithread example
1 parent c897b7b commit 7317f26

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../google_benchmark_cmake/helper.hpp
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../google_benchmark_cmake/multithread_bench.hpp
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef HELPER_HPP
2+
#define HELPER_HPP
3+
4+
#ifdef _MSC_VER
5+
#define NOINLINE __declspec(noinline)
6+
#else
7+
#define NOINLINE __attribute__((noinline))
8+
#endif
9+
10+
#include <benchmark/benchmark.h>
11+
12+
#include <cstdint>
13+
#include <numeric>
14+
#include <vector>
15+
16+
NOINLINE static uint64_t recursive_fib(int n) {
17+
if (n <= 1) return n;
18+
return recursive_fib(n - 1) + recursive_fib(n - 2);
19+
}
20+
21+
NOINLINE static uint64_t expensive_operation() {
22+
// Large memory allocation
23+
std::vector<uint64_t> data(1024 * 1024, 42); // 8 MiB allocation
24+
25+
// Expensive recursive computation that will dominate flamegraph
26+
uint64_t fib_result = recursive_fib(30);
27+
28+
// More expensive work
29+
uint64_t sum = std::accumulate(data.begin(), data.end(), uint64_t(0));
30+
benchmark::DoNotOptimize(sum);
31+
benchmark::DoNotOptimize(fib_result);
32+
33+
return sum + fib_result;
34+
}
35+
36+
#endif // HELPER_HPP

examples/google_benchmark_cmake/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "fibonacci_bench.hpp"
66
#include "fixture_bench.hpp"
7+
#include "multithread_bench.hpp"
78
#include "sleep_bench.hpp"
89
#include "template_bench.hpp"
910

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
3+
#include <benchmark/benchmark.h>
4+
5+
#include <atomic>
6+
#include <thread>
7+
8+
#include "helper.hpp"
9+
10+
// Simple multithreaded benchmark: spawn thread, do work, join
11+
static void BM_SimpleMultithread(benchmark::State& state) {
12+
for (auto _ : state) {
13+
state.PauseTiming();
14+
std::atomic<bool> work_start{false};
15+
std::atomic<bool> work_done{false};
16+
17+
std::thread worker([&]() {
18+
// Wait for the signal to start
19+
while (!work_start.load()) {
20+
std::this_thread::yield();
21+
}
22+
23+
// Do some CPU work
24+
benchmark::DoNotOptimize(expensive_operation());
25+
work_done.store(true);
26+
});
27+
state.ResumeTiming();
28+
29+
// Signal the worker to start
30+
work_start.store(true);
31+
32+
// Wait for worker to complete
33+
while (!work_done.load()) {
34+
std::this_thread::yield();
35+
}
36+
37+
worker.join();
38+
}
39+
}
40+
41+
BENCHMARK(BM_SimpleMultithread);

0 commit comments

Comments
 (0)