shanet shanet - 3 months ago 30
C Question

What is the `pthread_mutex_lock()` wake order with multiple threads waiting?

Suppose I have multiple threads blocking on a call to

pthread_mutex_lock()
. When the mutex becomes available, does the first thread that called
pthread_mutex_lock()
get the lock? That is, are calls to
pthread_mutex_lock()
in FIFO order? If not, what, if any, order are they in? Thanks!

Answer

When the mutex becomes available, does the first thread that called pthread_mutex_lock() get the lock?

No. One of the waiting threads gets a lock, but which one gets it is not determined.

FIFO order?

FIFO mutex is rather a pattern already. See Implementing a FIFO mutex in pthreads

Comments