3 pthread_mutex_t mutex1;
4 pthread_mutex_t mutex2;
5 pthread_t thread1, thread2;
7 static void * thread_doit1(void *forks_arg) {
8 pthread_mutex_lock(&mutex2);
9 pthread_mutex_lock(&mutex1);
10 pthread_mutex_unlock(&mutex1);
11 pthread_mutex_unlock(&mutex2);
15 static void * thread_doit2(void *forks_arg) {
16 pthread_mutex_lock(&mutex1);
17 pthread_mutex_lock(&mutex2);
18 pthread_mutex_unlock(&mutex2);
19 pthread_mutex_unlock(&mutex1);
23 int main(int argc, char* argv[]) {
24 pthread_mutex_init(&mutex1, NULL);
25 pthread_mutex_init(&mutex2, NULL);
27 pthread_create(&thread1, NULL, &thread_doit1, NULL);
28 pthread_create(&thread2, NULL, &thread_doit2, NULL);
30 pthread_join(thread1, NULL);
31 pthread_join(thread2, NULL);