Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / lib / libpthread / detach1 / detach1.c
blob9387d230caf2e41e2e3a6fba5eeee69ff3ce2ad3
1 /* $NetBSD$ */
3 #include <err.h>
4 #include <errno.h>
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
10 #define NITER 100000
12 void *func(void *);
13 int main(void);
15 void *
16 func(void *dummy)
19 return NULL;
22 int
23 main()
25 int i;
27 for (i = 0; i < NITER; i++) {
28 pthread_t t;
29 int error;
31 if (pthread_create(&t, NULL, func, NULL)) {
33 * sleep and retry once for the case that
34 * the child threads are not finished yet.
36 printf("%d sleeping...\n", i);
37 sleep(10);
38 if (pthread_create(&t, NULL, func, NULL))
39 err(1, "create");
42 if (i & 1)
43 sched_yield(); /* give a chance thread to finish */
44 if (pthread_detach(t))
45 err(1, "detach");
47 error = pthread_join(t, NULL);
48 if (error != ESRCH && error != EINVAL) {
49 printf("unexpected error %d\n", error);
50 exit(3);
54 exit(0);