// run the code a few times, occasionally it deadlocks #include #include static void * simple_thread(void *); pthread_mutex_t mutex_1= PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mutex_2= PTHREAD_MUTEX_INITIALIZER; int main() { pthread_t tid = 0; for (int i=0; i<10000; i++) { // create a thread pthread_create(&tid, 0, &simple_thread, 0); } pthread_mutex_lock(&mutex_1); // acquire mutex_1 pthread_mutex_lock(&mutex_2); // acquire mutex_2 pthread_mutex_unlock(&mutex_2); // release mutex_2 pthread_mutex_unlock(&mutex_1); // release mutex_1 pthread_join(tid, NULL); return 0; } static void * simple_thread(void * dummy) { pthread_mutex_lock(&mutex_2); // acquire mutex_2 pthread_mutex_lock(&mutex_1); // acquire mutex_1 for (int i=0; i<10; i++) printf("\n Made it here \n"); pthread_mutex_unlock(&mutex_1); // release mutex_1 pthread_mutex_unlock(&mutex_2); // release mutex_2 return NULL; }