diff options
author | Johannes Stoelp <johannes.stoelp@gmail.com> | 2024-01-23 00:42:53 +0100 |
---|---|---|
committer | Johannes Stoelp <johannes.stoelp@gmail.com> | 2024-01-23 00:42:53 +0100 |
commit | 2cee0b6c1225d96cfd46274071a6f158c411cae7 (patch) | |
tree | a52b514ef7cbff80b3caa9f2879e85fb911ee9cc /src/trace_profile/callgrind/cg_example.cc | |
parent | 42c4782598dae5728539b24f83ebafc3e33b7448 (diff) | |
download | notes-2cee0b6c1225d96cfd46274071a6f158c411cae7.tar.gz notes-2cee0b6c1225d96cfd46274071a6f158c411cae7.zip |
callgrind: initial notes
Diffstat (limited to 'src/trace_profile/callgrind/cg_example.cc')
-rw-r--r-- | src/trace_profile/callgrind/cg_example.cc | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/trace_profile/callgrind/cg_example.cc b/src/trace_profile/callgrind/cg_example.cc new file mode 100644 index 0000000..d143790 --- /dev/null +++ b/src/trace_profile/callgrind/cg_example.cc @@ -0,0 +1,36 @@ +#include <cstdio> +#include <thread> + +#include <valgrind/callgrind.h> + +struct cnt { + volatile int a; + volatile int b; +}; + +void inc_a(cnt &c) { c.a++; } +void inc_b(cnt &c) { c.b++; } + +int main() { + cnt C{0, 0}; + + CALLGRIND_START_INSTRUMENTATION; + + std::thread T1([&C]() { + for (int i = 0; i < 20000000; ++i) { + inc_a(C); + } + }); + std::thread T2([&C]() { + for (int i = 0; i < 10000000; ++i) { + inc_b(C); + } + }); + T1.join(); + T2.join(); + + CALLGRIND_STOP_INSTRUMENTATION; + + printf("%d %d\n", C.a, C.b); + return 0; +} |