10 void *threadfunc(void *arg
);
12 pthread_mutex_t mutex
;
15 main(int argc
, char *argv
[])
19 pthread_mutexattr_t mattr
;
22 printf("1: Mutex-test 4\n");
24 pthread_mutexattr_init(&mattr
);
25 pthread_mutexattr_settype(&mattr
, PTHREAD_MUTEX_RECURSIVE
);
27 pthread_mutex_init(&mutex
, &mattr
);
29 pthread_mutexattr_destroy(&mattr
);
32 pthread_mutex_lock(&mutex
);
33 ret
= pthread_create(&new, NULL
, threadfunc
, &x
);
35 err(1, "pthread_create");
37 printf("1: Before recursively acquiring the mutex.\n");
38 ret
= pthread_mutex_lock(&mutex
);
40 err(1, "pthread_mutex_lock recursive");
42 printf("1: Before releasing the mutex once.\n");
44 pthread_mutex_unlock(&mutex
);
45 printf("1: After releasing the mutex once.\n");
49 printf("1: Before releasing the mutex twice.\n");
51 pthread_mutex_unlock(&mutex
);
52 printf("1: After releasing the mutex twice.\n");
54 ret
= pthread_join(new, &joinval
);
56 err(1, "pthread_join");
58 pthread_mutex_lock(&mutex
);
59 printf("1: Thread joined. X was %d. Return value (int) was %d\n",
62 assert(*(int *)joinval
== 21);
63 pthread_mutex_unlock(&mutex
);
72 printf("2: Second thread.\n");
75 printf("2: Locking mutex\n");
76 pthread_mutex_lock(&mutex
);
77 printf("2: Got mutex. *param = %d\n", *param
);
80 pthread_mutex_unlock(&mutex
);