Race condition in pthread_mutex_timedlock?
Mark Geisert
mark@maxrnd.com
Thu Oct 22 23:22:07 GMT 2026
Hello Luca,
On 10/20/2026 7:36 AM, Luca Bacci wrote:
> 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?
Just speaking for myself, I don't know offhand if you're missing
something or not and I don't have time to investigate.
I invite you to write (i.e., code) what we call an STC (Simple Test
Case) that tests the situation you raise. In other words, try to break
the existing Cygwin code. Write it so you can vary the number of
threads. Use assert() to test conditions along the way. If you do find
an issue then we can investigate. I can also then offer to test your
STC on a 16C/32T machine with dozens or hundreds of threads :-).
Regards,
..mark
More information about the Cygwin-developers
mailing list