46 lines · cpp
1// RUN: %clangxx_tsan %s -o %t2// RUN: %run %t 2>&1 | FileCheck %s3 4// bench.h needs pthread barriers which are not available on OS X5// UNSUPPORTED: darwin6 7#include "bench.h"8 9void *nop_thread(void *arg) {10 pthread_setname_np(pthread_self(), "nop_thread");11 return nullptr;12}13 14void thread(int tid) {15 for (int i = 0; i < bench_niter; i++) {16 pthread_t th;17 pthread_create(&th, nullptr, nop_thread, nullptr);18 pthread_join(th, nullptr);19 }20}21 22void bench() {23 // Benchmark thread creation/joining in presence of a large number24 // of threads (both alive and already joined).25 printf("starting transient threads...\n");26 for (int i = 0; i < 200; i++) {27 const int kBatch = 100;28 pthread_t th[kBatch];29 for (int j = 0; j < kBatch; j++)30 pthread_create(&th[j], nullptr, nop_thread, nullptr);31 for (int j = 0; j < kBatch; j++)32 pthread_join(th[j], nullptr);33 }34 printf("starting persistent threads...\n");35 const int kLiveThreads = 2000;36 pthread_t th[kLiveThreads];37 for (int j = 0; j < kLiveThreads; j++)38 pthread_create(&th[j], nullptr, nop_thread, nullptr);39 printf("starting benchmark threads...\n");40 start_thread_group(bench_nthread, thread);41 for (int j = 0; j < kLiveThreads; j++)42 pthread_join(th[j], nullptr);43}44 45// CHECK: DONE46