I have a question about pthread_wrlock_wrlock and pthread_wrlock_rdlock:
Are they not implemented as spin locks??
I tried this in my code:
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_rdlock (&rwlock);
pthread_rwlock_wrlock (&rwlock);
=> this causes deadlock as I expected
However, I don’t understand why the revered one doesn’t cause deadlock:
pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
pthread_rwlock_wrlock (&rwlock); // return 0
pthread_rwlock_rdlock (&rwlock); // return 35
If pthread_rwlock_rdlock spins, why does it return failure rather than spin?
Because it is the same thread, it would produce a deadlock hence the return code
EDEADLKwhich is definitely not what you want.The first case looks like a defect as it should also return
EDEADLK. Blocking should only occur in separately threads.