xref: /llvm-project/compiler-rt/test/profile/ContinuousSyncMode/multi-threaded.cpp (revision de294c968bf292794ca9f0a6a481d3dff3bcc2eb)
1*de294c96SNAKAMURA Takumi // REQUIRES: continuous-mode
2a17f11baSWael Yehia 
3a17f11baSWael Yehia // RUN: rm -f %t.profraw
4a17f11baSWael Yehia // RUN: %clangxx_pgogen_cont -lpthread %s -o %t.exe -mllvm -disable-vp -fprofile-update=atomic
5a17f11baSWael Yehia // RUN: env LLVM_PROFILE_FILE="%c%t.profraw" %run %t.exe
6a17f11baSWael Yehia // RUN: llvm-profdata show --counts --function=accum  %t.profraw | FileCheck %s
7a17f11baSWael Yehia // CHECK:    Block counts: [100000, 4]
8a17f11baSWael Yehia 
9a17f11baSWael Yehia #include <thread>
10a17f11baSWael Yehia 
11a17f11baSWael Yehia int x = 0;
12a17f11baSWael Yehia void accum(int n) {
13a17f11baSWael Yehia   for (int i = 0; i < n; i++)
14a17f11baSWael Yehia     x += i; // don't care about accuracy, no need for atomic.
15a17f11baSWael Yehia }
16a17f11baSWael Yehia 
17a17f11baSWael Yehia int main() {
18a17f11baSWael Yehia   int init_value = 10000;
19a17f11baSWael Yehia   auto t1 = std::thread(accum, 1*init_value);
20a17f11baSWael Yehia   auto t2 = std::thread(accum, 2*init_value);
21a17f11baSWael Yehia   auto t3 = std::thread(accum, 3*init_value);
22a17f11baSWael Yehia   auto t4 = std::thread(accum, 4*init_value);
23a17f11baSWael Yehia 
24a17f11baSWael Yehia   t1.join();
25a17f11baSWael Yehia   t2.join();
26a17f11baSWael Yehia   t3.join();
27a17f11baSWael Yehia   t4.join();
28a17f11baSWael Yehia   return !x;
29a17f11baSWael Yehia }
30