1 /** Initialize several kinds of mutexes and lock each mutex twice.
2 * Note: locking a regular mutex twice causes a deadlock.
10 #include "../../config.h"
13 static void lock_twice(pthread_mutex_t
* const p
)
15 if (pthread_mutex_trylock(p
) != 0)
16 fprintf(stderr
, "first lock call failed !\n");
17 if (pthread_mutex_trylock(p
) != 0)
18 fprintf(stderr
, "second lock call failed !\n");
19 if (pthread_mutex_unlock(p
) != 0)
20 fprintf(stderr
, "first unlock call failed !\n");
21 if (pthread_mutex_unlock(p
) != 0)
22 fprintf(stderr
, "second unlock call failed !\n");
25 int main(int argc
, char** argv
)
27 #if defined(HAVE_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
29 pthread_mutex_t m
= PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
;
31 fprintf(stderr
, "Recursive mutex (statically initialized).\n");
33 pthread_mutex_destroy(&m
);
36 #if defined(HAVE_PTHREAD_MUTEX_RECURSIVE_NP)
39 pthread_mutexattr_t attr
;
41 fprintf(stderr
, "\nRecursive mutex (initialized via mutex attributes).\n");
42 pthread_mutexattr_init(&attr
);
43 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_RECURSIVE_NP
);
44 pthread_mutex_init(&m
, &attr
);
45 pthread_mutexattr_destroy(&attr
);
47 pthread_mutex_destroy(&m
);
50 #if defined(HAVE_PTHREAD_MUTEX_ERRORCHECK_NP)
53 pthread_mutexattr_t attr
;
55 fprintf(stderr
, "\nError checking mutex.\n");
56 pthread_mutexattr_init(&attr
);
57 pthread_mutexattr_settype(&attr
, PTHREAD_MUTEX_ERRORCHECK_NP
);
58 pthread_mutex_init(&m
, &attr
);
59 pthread_mutexattr_destroy(&attr
);
61 pthread_mutex_destroy(&m
);
66 pthread_mutex_t m
= PTHREAD_MUTEX_INITIALIZER
;
68 fprintf(stderr
, "\nNon-recursive mutex.\n");
72 fprintf(stderr
, "\nDone.\n");