I was curious whether pthread_spin_lock() can really detect deadlock scenario, so I wrote a simple program to test:
#include <stdio.h>
#include <pthread.h>
int
main(void)
{
pthread_spinlock_t lock;
if (pthread_spin_init(&lock, PTHREAD_PROCESS_PRIVATE) != 0) {
perror("pthread_spin_init error");
return 1;
}
if (pthread_spin_lock(&lock) != 0) {
perror("pthread_spin_lock 1 error");
return 1;
}
if (pthread_spin_lock(&lock) != 0) {
perror("pthread_spin_lock 2 error");
return 1;
}
return 0;
}
Tested it on both Linux
and FreeBSD
, the program blocked on the second pthread_spin_lock
, never return:
$ ./double_lock
P.S., the code can be found here.