Reputation: 18414
I wrote a small program that works perfectly fine until it's being dynamically instrumented by Callgrind:
$ g++ -std=c++11 -pthread -g -ggdb -o program.exe program.cpp
$ time valgrind --tool=callgrind ./program.exe
The code:
#include <atomic>
#include <thread>
#include <iostream>
constexpr int CST_TARGET = 10*1000;
std::atomic<bool> g_lock = {false};
std::atomic<bool> g_got_work = {true};
int g_passer = 0;
long long g_total = 0;
void producer() {
while (1) {
while (g_lock.load(std::memory_order_seq_cst));
if (g_passer >= CST_TARGET) {
g_got_work.store(false, std::memory_order_seq_cst);
return;
}
++g_passer;
g_lock.store(true, std::memory_order_seq_cst);
}
}
void consumer() {
while (g_got_work.load(std::memory_order_seq_cst)) {
if (g_lock.load(std::memory_order_seq_cst)) {
g_total += g_passer;
g_lock.store(false, std::memory_order_seq_cst);
}
}
}
int main() {
std::atomic<int> val(0);
std::thread t1(producer);
std::thread t2(consumer);
t1.join();
t2.join();
std::cout << "g_passer = " << g_passer << std::endl;
std::cout << "g_total = " << g_total << std::endl;
return 0;
}
The instrumentation won't end after 10 mins, so I terminated it and had a look at KCachegrind stats. There are hundreds of millions to billions of calls to std::atomic<bool>::load(...)
.
Any ideas which parts of Callgrind altered the behaviour of atomic calls and failed them? The program itself runs in milliseconds without Callgrind.
Upvotes: 0
Views: 148