1 // A benchmark that executes malloc/free pairs in parallel.
2 // Usage: ./a.out number_of_threads total_number_of_allocations
3 // RUN: %clangxx_lsan %s -o %t
4 // RUN: %env_lsan_opts=use_ld_allocations=0 %run %t 5 1000000 2>&1
12 const int kMaxNumThreads
= 5000;
13 pthread_t tid
[kMaxNumThreads
];
15 pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
16 pthread_mutex_t mutex
= PTHREAD_MUTEX_INITIALIZER
;
19 void *thread_fun(void *arg
) {
20 pthread_mutex_lock(&mutex
);
21 while (!go
) pthread_cond_wait(&cond
, &mutex
);
22 pthread_mutex_unlock(&mutex
);
23 for (int i
= 0; i
< total_num_alloc
/ num_threads
; i
++) {
25 __asm__
__volatile__("" : : "r"(p
) : "memory");
31 int main(int argc
, char** argv
) {
33 num_threads
= atoi(argv
[1]);
34 assert(num_threads
> 0);
35 assert(num_threads
<= kMaxNumThreads
);
36 total_num_alloc
= atoi(argv
[2]);
37 assert(total_num_alloc
> 0);
38 printf("%d threads, %d allocations in each\n", num_threads
,
39 total_num_alloc
/ num_threads
);
40 for (int i
= 0; i
< num_threads
; i
++)
41 pthread_create(&tid
[i
], 0, thread_fun
, 0);
42 pthread_mutex_lock(&mutex
);
44 pthread_cond_broadcast(&cond
);
45 pthread_mutex_unlock(&mutex
);
46 for (int i
= 0; i
< num_threads
; i
++) pthread_join(tid
[i
], 0);