Path: blob/main/contrib/llvm-project/compiler-rt/lib/tsan/benchmarks/mini_bench_local.cpp
35266 views
// Mini-benchmark for tsan: non-shared memory writes.1#include <pthread.h>2#include <stdio.h>3#include <stdlib.h>4#include <assert.h>56int len;7int *a;8const int kNumIter = 1000;910__attribute__((noinline))11void Run(int idx) {12for (int i = 0, n = len; i < n; i++)13a[i + idx * n] = i;14}1516void *Thread(void *arg) {17long idx = (long)arg;18printf("Thread %ld started\n", idx);19for (int i = 0; i < kNumIter; i++)20Run(idx);21printf("Thread %ld done\n", idx);22return 0;23}2425int main(int argc, char **argv) {26int n_threads = 0;27if (argc != 3) {28n_threads = 4;29len = 1000000;30} else {31n_threads = atoi(argv[1]);32assert(n_threads > 0 && n_threads <= 32);33len = atoi(argv[2]);34}35printf("%s: n_threads=%d len=%d iter=%d\n",36__FILE__, n_threads, len, kNumIter);37a = new int[n_threads * len];38pthread_t *t = new pthread_t[n_threads];39for (int i = 0; i < n_threads; i++) {40pthread_create(&t[i], 0, Thread, (void*)i);41}42for (int i = 0; i < n_threads; i++) {43pthread_join(t[i], 0);44}45delete [] t;46delete [] a;47return 0;48}495051