1 /* $NetBSD: preempt1.c,v 1.2 2003/06/27 13:27:58 skrll Exp $ */
11 void *threadfunc(void *arg
);
13 pthread_mutex_t mutex
;
17 #define HUGE_BUFFER 1<<20
21 main(int argc
, char *argv
[])
30 mem
= malloc(HUGE_BUFFER
);
34 fd
= open("/dev/urandom", O_RDONLY
, 0);
38 printf("1: preempt test\n");
40 pthread_cond_init(&cond
, NULL
);
41 pthread_mutex_init(&mutex
, NULL
);
43 pthread_mutex_lock(&mutex
);
47 for (i
= 0; i
< NTHREADS
; i
++) {
48 ret
= pthread_create(&new, NULL
, threadfunc
, NULL
);
50 err(1, "pthread_create");
53 while (started
< NTHREADS
) {
54 pthread_cond_wait(&cond
, &mutex
);
57 printf("1: Thread has started.\n");
59 pthread_mutex_unlock(&mutex
);
60 printf("1: After releasing the mutex.\n");
62 ret
= read(fd
, mem
, HUGE_BUFFER
);
65 assert(ret
== HUGE_BUFFER
);
67 ret
= pthread_join(new, &joinval
);
69 err(1, "pthread_join");
71 printf("1: Thread joined.\n");
79 printf("2: Second thread.\n");
81 printf("2: Locking mutex\n");
82 pthread_mutex_lock(&mutex
);
83 printf("2: Got mutex.\n");
86 pthread_mutex_unlock(&mutex
);
87 pthread_cond_signal(&cond
);