1// RUN: %clang_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
2#include "test.h"
3
4void *thr(void *arg) {
5 // Create a sync object on stack, so there is something to free on thread end.
6 volatile int x;
7 __atomic_fetch_add(&x, 1, __ATOMIC_SEQ_CST);
8 barrier_wait(barrier: &barrier);
9 return 0;
10}
11
12int main() {
13 const int kThreads = 300;
14 const int kIters = 10;
15 barrier_init(barrier: &barrier, count: kThreads + 1);
16 pthread_attr_t attr;
17 pthread_attr_init(attr: &attr);
18 pthread_attr_setstacksize(attr: &attr, stacksize: 16 << 20);
19 for (int iter = 0; iter < kIters; iter++) {
20 pthread_t threads[kThreads];
21 for (int t = 0; t < kThreads; t++) {
22 int err = pthread_create(newthread: &threads[t], attr: &attr, start_routine: thr, arg: 0);
23 if (err) {
24 fprintf(stderr, format: "Failed to create thread #%d\n", t);
25 return 1;
26 }
27 }
28 barrier_wait(barrier: &barrier);
29 for (int t = 0; t < kThreads; t++)
30 pthread_join(th: threads[t], thread_return: 0);
31 }
32 pthread_attr_destroy(attr: &attr);
33 fprintf(stderr, format: "DONE\n");
34 return 0;
35}
36
37// CHECK: DONE
38
39

source code of compiler-rt/test/tsan/lots_of_threads.c