1// Mini-benchmark for tsan: non-shared memory writes.
2#include <pthread.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <assert.h>
6
7int len;
8int *a;
9const int kNumIter = 1000;
10
11__attribute__((noinline))
12void Run(int idx) {
13 for (int i = 0, n = len; i < n; i++)
14 a[i + idx * n] = i;
15}
16
17void *Thread(void *arg) {
18 long idx = (long)arg;
19 printf(format: "Thread %ld started\n", idx);
20 for (int i = 0; i < kNumIter; i++)
21 Run(idx);
22 printf(format: "Thread %ld done\n", idx);
23 return 0;
24}
25
26int main(int argc, char **argv) {
27 int n_threads = 0;
28 if (argc != 3) {
29 n_threads = 4;
30 len = 1000000;
31 } else {
32 n_threads = atoi(nptr: argv[1]);
33 assert(n_threads > 0 && n_threads <= 32);
34 len = atoi(nptr: argv[2]);
35 }
36 printf(format: "%s: n_threads=%d len=%d iter=%d\n",
37 __FILE__, n_threads, len, kNumIter);
38 a = new int[n_threads * len];
39 pthread_t *t = new pthread_t[n_threads];
40 for (int i = 0; i < n_threads; i++) {
41 pthread_create(newthread: &t[i], attr: 0, start_routine: Thread, arg: (void*)i);
42 }
43 for (int i = 0; i < n_threads; i++) {
44 pthread_join(th: t[i], thread_return: 0);
45 }
46 delete [] t;
47 delete [] a;
48 return 0;
49}
50

source code of compiler-rt/lib/tsan/benchmarks/mini_bench_local.cpp