1// Simple stress test for of pthread_create. Increase arg to use as benchmark.
2
3// RUN: %clangxx -O3 -pthread %s -o %t && %run %t 10
4
5// Crashes on Android.
6// UNSUPPORTED: android
7
8#include <cstdint>
9#include <pthread.h>
10#include <stdlib.h>
11
12extern "C" const char *__asan_default_options() {
13 // 32bit asan can allocate just a few FakeStacks.
14 return sizeof(void *) < 8 ? "detect_stack_use_after_return=0" : "";
15}
16
17static void *null_func(void *args) { return nullptr; }
18
19static void *loop(void *args) {
20 uintptr_t n = (uintptr_t)args;
21 for (int i = 0; i < n; ++i) {
22 pthread_t thread;
23 if (pthread_create(newthread: &thread, attr: 0, start_routine: null_func, arg: nullptr) == 0)
24 pthread_detach(th: thread);
25 }
26 return nullptr;
27}
28
29int main(int argc, char **argv) {
30 uintptr_t n = atoi(nptr: argv[1]);
31 pthread_t threads[64];
32 for (auto &thread : threads)
33 while (pthread_create(newthread: &thread, attr: 0, start_routine: loop, arg: (void *)n) != 0) {
34 }
35
36 for (auto &thread : threads)
37 pthread_join(th: thread, thread_return: nullptr);
38 return 0;
39}
40

source code of compiler-rt/test/sanitizer_common/TestCases/Posix/create_thread_loop2.cpp