Race condition in pthread_mutex_timedlock?

Luca Bacci luca.bacci@outlook.com
Tue Oct 20 14:36:04 GMT 2026


Hello,

Current implementation of pthread_mutex_timelock() seems racy; if timedwait() times
while unlock() is running on another thread, the mutex can be left unlocked and
with the internal event set.

Consider this:

Thread A                  Thread B

   timedlock()
     counter 0 -> 1

                             timedlock()
                               counter 1 -> 2
                               (waits with timeout)



   unlock()
     counter 2 -> 1
                               (resumes due to timeout)
                               counter 1 -> 0
     set event

Now the mutex is unlocked but the internal event is left in the signaled state. Whenever contention occurs
again, two threads will acquire the lock.

Am I missing something?

Best Regards,
Luca



More information about the Cygwin-developers mailing list