aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/trace_profile/callgrind/cg_example.cc
blob: d1437905eb12f4bbc08a4844f477882bc5760b2d (plain) (blame)
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
33
34
35
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;
}