1 /* $NetBSD: mutex2.c,v 1.1 2003/01/30 18:05:26 thorpej Exp $ */
9 void *threadfunc(void *arg
);
11 pthread_mutex_t mutex
;
15 main(int argc
, char *argv
[])
17 int count
, count2
, ret
;
21 printf("1: Mutex-test 2\n");
23 ret
= pthread_mutex_init(&mutex
, NULL
);
25 err(1, "pthread_mutex_init");
27 count
= count2
= 10000000;
28 ret
= pthread_mutex_lock(&mutex
);
30 err(1, "pthread_mutex_lock (1)");
31 ret
= pthread_create(&new, NULL
, threadfunc
, &count2
);
33 err(1, "pthread_create");
35 printf("1: Thread %p\n", pthread_self());
36 ret
= pthread_mutex_unlock(&mutex
);
38 err(1, "pthread_mutex_unlock(1)");
42 ret
= pthread_mutex_lock(&mutex
);
44 err(1, "pthread_mutex_lock(2)");
46 ret
= pthread_mutex_unlock(&mutex
);
48 err(1, "pthread_mutex_unlock(2)");
51 ret
= pthread_join(new, &joinval
);
53 err(1, "pthread_join");
55 pthread_mutex_lock(&mutex
);
56 printf("1: Thread joined. X was %d. Return value (long) was %ld\n",
58 assert(x
== 20000000);
65 long count
= *(int *)arg
;
68 printf("2: Second thread (%p). Count is %ld\n", pthread_self(), count
);
71 ret
= pthread_mutex_lock(&mutex
);
73 err(1, "pthread_mutex_lock(3)");
75 ret
= pthread_mutex_unlock(&mutex
);
77 err(1, "pthread_mutex_unlock(3)");