47 lines · cpp
1// This file deliberately uses low level linux-specific API for thread creation because:2// - instruction-stepping over thread creation using higher-level functions was very slow3// - it was also unreliable due to single-stepping bugs unrelated to this test4// - some threading libraries do not create or destroy threads when we would expect them to5 6#include <sched.h>7 8#include <atomic>9#include <cstdio>10 11enum { STACK_SIZE = 0x2000 };12 13static uint8_t child_stack[STACK_SIZE];14 15pid_t child_tid;16 17std::atomic<bool> flag(false);18 19int thread_main(void *)20{21 while (! flag) // Make sure the thread does not exit prematurely22 ;23 24 return 0;25}26 27int main ()28{29 int ret = clone(thread_main,30 child_stack + STACK_SIZE/2, // Don't care whether the stack grows up or down,31 // just point to the middle32 CLONE_CHILD_CLEARTID | CLONE_FILES | CLONE_FS | CLONE_PARENT_SETTID |33 CLONE_SIGHAND | CLONE_SYSVSEM | CLONE_THREAD | CLONE_VM,34 nullptr, // thread_main argument35 &child_tid);36 37 if (ret == -1)38 {39 perror("clone");40 return 1;41 }42 43 flag = true;44 45 return 0;46}47