xref: /aosp_15_r20/system/extras/simpleperf/runtest/function_pthread.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker #include <pthread.h>
2*288bf522SAndroid Build Coastguard Worker #include <stdio.h>
3*288bf522SAndroid Build Coastguard Worker #include <stdlib.h>
4*288bf522SAndroid Build Coastguard Worker #include <string.h>
5*288bf522SAndroid Build Coastguard Worker 
6*288bf522SAndroid Build Coastguard Worker constexpr int LOOP_COUNT = 100000000;
7*288bf522SAndroid Build Coastguard Worker 
ChildThreadFunction(void *)8*288bf522SAndroid Build Coastguard Worker void* ChildThreadFunction(void*) {
9*288bf522SAndroid Build Coastguard Worker   for (volatile int i = 0; i < LOOP_COUNT; ++i) {
10*288bf522SAndroid Build Coastguard Worker   }
11*288bf522SAndroid Build Coastguard Worker   return nullptr;
12*288bf522SAndroid Build Coastguard Worker }
13*288bf522SAndroid Build Coastguard Worker 
MainThreadFunction()14*288bf522SAndroid Build Coastguard Worker void MainThreadFunction() {
15*288bf522SAndroid Build Coastguard Worker   for (volatile int i = 0; i < LOOP_COUNT; ++i) {
16*288bf522SAndroid Build Coastguard Worker   }
17*288bf522SAndroid Build Coastguard Worker }
18*288bf522SAndroid Build Coastguard Worker 
main()19*288bf522SAndroid Build Coastguard Worker int main() {
20*288bf522SAndroid Build Coastguard Worker   while (true) {
21*288bf522SAndroid Build Coastguard Worker     pthread_t thread;
22*288bf522SAndroid Build Coastguard Worker     int ret = pthread_create(&thread, nullptr, ChildThreadFunction, nullptr);
23*288bf522SAndroid Build Coastguard Worker     if (ret != 0) {
24*288bf522SAndroid Build Coastguard Worker       fprintf(stderr, "pthread_create failed: %s\n", strerror(ret));
25*288bf522SAndroid Build Coastguard Worker       exit(1);
26*288bf522SAndroid Build Coastguard Worker     }
27*288bf522SAndroid Build Coastguard Worker     MainThreadFunction();
28*288bf522SAndroid Build Coastguard Worker     ret = pthread_join(thread, nullptr);
29*288bf522SAndroid Build Coastguard Worker     if (ret != 0) {
30*288bf522SAndroid Build Coastguard Worker       fprintf(stderr, "pthread_join failed: %s\n", strerror(ret));
31*288bf522SAndroid Build Coastguard Worker       exit(1);
32*288bf522SAndroid Build Coastguard Worker     }
33*288bf522SAndroid Build Coastguard Worker   }
34*288bf522SAndroid Build Coastguard Worker   return 0;
35*288bf522SAndroid Build Coastguard Worker }
36