In the library pthread.h
are pthread_rwlock_t
FIFO?
In the following example we have multiple threads. Imagine that every thread is guaranteed to run in order.
// Thread 1 - does a write lock
pthread_rwlock_wrlock(&lock);
// Thread 2 - does a read but has to wait for 1
pthread_rwlock_rdlock(&lock);
// Thread 3 - does a read but has to wait for 1
pthread_rwlock_rdlock(&lock);
// Thread 4 - does a write but has to wait for 1
pthread_rwlock_wrlock(&lock);
// Thread 1 - unlocks
pthread_rwlock_unlock(&lock);
// who gets the lock?
After thread 1 releases the lock, who gets the lock? Is it guaranteed that thread 2 and 3 do? Or could it possibly be given to 4?
Again, Imagine that every thread is guaranteed to run in order and thread 1 doesn't release the lock until all threads have tried to obtain the lock.
I did some research and found this document from the Oracle website that explains the scheduling policy for pthread read-write locks.
If the call to the pthread_rwlock_unlock() results in the read-write lock object becoming unlocked and there are multiple threads waiting to acquire the read-write lock object for writing, the scheduling policy is used to determine which thread acquires the read-write lock object for writing. If there are multiple threads waiting to acquire the read-write lock object for reading, the scheduling policy is used to determine the order in which the waiting threads acquire the read-write lock object for reading. If there are multiple threads blocked on rwlock for both read locks and write locks, it is unspecified whether the readers acquire the lock first or whether a writer acquires the lock first.
So in conclusion, they are not guaranteed to be FIFO.