pthread读写锁是FIFO吗?

Are pthread read-write locks FIFO?

图书馆pthread.hpthread_rwlock_t先进先出?

在下面的例子中我们有多个线程。想象一下,保证每个线程按顺序 运行。

// 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?

线程1释放锁后,谁拿到了锁?是否保证线程 2 和线程 3 可以?还是可以给4?

同样,想象一下,保证每个线程按顺序 运行 并且线程 1 在所有线程都尝试获取锁之前不会释放锁。

我做了一些研究,发现 this document from the Oracle website 解释了 pthread 读写锁的调度策略。

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.

所以综上所述,他们并不能保证是先进先出的。