1 /* Creates several daemon threads and non-daemon threads.
2 Tests that the process can exit even if the daemon threads are still running,
3 as per thr_create(3C). */
12 #define DAEMON_THREADS 5
13 #define NON_DAEMON_THREADS 6
14 #define SLEEP_100_MS usleep(100 * 1000)
16 static pthread_barrier_t barrier
;
18 void *daemon_thread_func(void *arg
) {
19 size_t index
= (size_t) arg
;
20 printf("DAEMON thread #%zu running\n", index
); fflush(stdout
);
21 pthread_barrier_wait(&barrier
);
23 /* Give the non-daemon threads enough time to exit. */
25 printf("DAEMON thread #%zu still running?!\n", index
); fflush(stdout
);
29 void *normal_thread_func(void *arg
) {
30 size_t index
= (size_t) arg
;
31 printf("non-daemon thread #%zu running\n", index
); fflush(stdout
);
32 pthread_barrier_wait(&barrier
);
40 int ret
= pthread_barrier_init(&barrier
, NULL
,
41 DAEMON_THREADS
+ NON_DAEMON_THREADS
+ 1);
43 fprintf(stderr
, "pthread_barrier_init failed: %s\n", strerror(ret
));
47 for (i
= 0; i
< DAEMON_THREADS
; i
++) {
48 ret
= thr_create(NULL
, 0, daemon_thread_func
, (void *) i
,
51 fprintf(stderr
, "thr_create failed: %s\n", strerror(ret
));
57 for (i
= 0; i
< NON_DAEMON_THREADS
; i
++) {
58 ret
= thr_create(NULL
, 0, normal_thread_func
, (void *) i
, 0, NULL
);
60 fprintf(stderr
, "thr_create failed: %s\n", strerror(ret
));
66 pthread_barrier_wait(&barrier
);
68 printf("MAIN thread exiting\n");
69 /* Exit only the main thread, not whole process.
70 That is, do not exit(0) or return(0). */