I was working on a hobby project where mutexes were behaving mysteriously. I boiled it down to this test case that should obviously deadlock.
#include <pthread.h>
#include <stdio.h>
int main() {
pthread_mutex_t test;
pthread_mutex_init(&test, NULL);
pthread_mutex_lock(&test);
pthread_mutex_lock(&test);
printf("Took lock twice\n");
return 0;
}
However, when I compile without the -lpthread
flag, not only does the program still compile and link, it also runs without deadlocking. Why?
gcc pthread_break.c -o pthread_test
./pthread_test
Took lock twice
Compiling with the -lpthread
flag yields the expected result:
gcc pthread_break.c -o pthread_test -lpthread
./pthread_test
<- deadlocked here
I'm running GCC version 7.2.0.