Update potential misunderstanding about thread safety
This commit is contained in:
@@ -58,9 +58,6 @@ struct ContentionEnvironment {
|
||||
bg_counter.inc(1.0);
|
||||
bg_gauge.set(dist(rng));
|
||||
bg_histogram.observe(dist(rng));
|
||||
|
||||
// Small delay to avoid spinning too fast
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(1));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -74,8 +71,6 @@ struct ContentionEnvironment {
|
||||
auto output = metric::render(arena);
|
||||
static_cast<void>(output); // Suppress unused variable warning
|
||||
arena.reset();
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(100));
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -140,9 +135,6 @@ int main() {
|
||||
// Start background threads creating contention
|
||||
env.start_background_contention(8);
|
||||
|
||||
std::this_thread::sleep_for(
|
||||
std::chrono::milliseconds(100)); // Let background threads start
|
||||
|
||||
bench.run("counter.inc() - 8 background threads", [&]() {
|
||||
env.counter.inc(1.0);
|
||||
ankerl::nanobench::doNotOptimizeAway(env.counter);
|
||||
@@ -172,8 +164,6 @@ int main() {
|
||||
env.start_background_contention(4);
|
||||
env.start_render_thread();
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
|
||||
bench.run("counter.inc() - with concurrent render", [&]() {
|
||||
env.counter.inc(1.0);
|
||||
ankerl::nanobench::doNotOptimizeAway(env.counter);
|
||||
@@ -192,29 +182,28 @@ int main() {
|
||||
|
||||
// Shared gauge contention
|
||||
{
|
||||
// Test the multi-writer CAS behavior of gauges
|
||||
// Test the multi-writer CAS behavior of gauges when multiple threads
|
||||
// create gauges with the same labels. They will all point to the same
|
||||
// underlying state, causing high contention.
|
||||
auto gauge_family =
|
||||
metric::create_gauge("shared_gauge", "Shared gauge test");
|
||||
auto shared_gauge = gauge_family.create({{"shared", "true"}});
|
||||
|
||||
// Background threads all writing to the SAME gauge (high CAS contention)
|
||||
std::atomic<bool> stop_shared{false};
|
||||
std::vector<std::thread> shared_threads;
|
||||
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
shared_threads.emplace_back([&shared_gauge, &stop_shared]() {
|
||||
shared_threads.emplace_back([&gauge_family, &stop_shared]() {
|
||||
auto gauge = gauge_family.create({{"shared", "true"}});
|
||||
while (!stop_shared.load(std::memory_order_relaxed)) {
|
||||
shared_gauge.inc(1.0);
|
||||
std::this_thread::sleep_for(std::chrono::nanoseconds(100));
|
||||
gauge.inc(1.0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
|
||||
bench.run("gauge.inc() - 8 threads same gauge (CAS contention)", [&]() {
|
||||
shared_gauge.inc(1.0);
|
||||
ankerl::nanobench::doNotOptimizeAway(shared_gauge);
|
||||
auto gauge_for_benchmark = gauge_family.create({{"shared", "true"}});
|
||||
bench.run("gauge.inc() - 8 threads, same labels (contention)", [&]() {
|
||||
gauge_for_benchmark.inc(1.0);
|
||||
ankerl::nanobench::doNotOptimizeAway(gauge_for_benchmark);
|
||||
});
|
||||
|
||||
stop_shared.store(true);
|
||||
@@ -292,7 +281,6 @@ int main() {
|
||||
while (!stop_callback.load()) {
|
||||
counter_value.fetch_add(1);
|
||||
gauge_value.store(gauge_value.load() + 1);
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(10));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user