intmain() { for (int i = 0; i < 10; i++) { create(Thello); } }
1 2 3 4 5 6 7 8 9 10
Hello from thread #1 Hello from thread #2 Hello from thread #3 Hello from thread #4 Hello from thread #5 Hello from thread #6 Hello from thread #7 Hello from thread #8 Hello from thread #9 Hello from thread #A
__thread char *base, *cur; // thread-local variables __thread int id;
// objdump to see how thread-local variables are implemented __attribute__((noinline)) voidset_cur(void *ptr) { cur = ptr; } __attribute__((noinline)) char *get_cur() { return cur; }
voidstackoverflow(int n) { set_cur(&n); if (n % 1024 == 0) { int sz = base - get_cur(); printf("Stack size of T%d >= %d KB\n", id, sz / 1024); } stackoverflow(n + 1); }
voidTprobe(int tid) { id = tid; base = (void *)&tid; stackoverflow(0); }
intmain() { setbuf(stdout, NULL); for (int i = 0; i < 4; i++) { create(Tprobe); } }