Race condition in pthread_mutex_timedlock?

Luca Bacci luca.bacci@outlook.com
Fri Oct 23 11:57:26 GMT 2026


Yeah, that's a good idea :) See the attached STC -- that sample triggers 
the error quite frequently here (YMMV!)


Best Regards!

Luca

On 10/23/2026 1:22 AM, Mark Geisert wrote:
> 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
-------------- next part --------------
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <errno.h>
#include <assert.h>
#include <pthread.h>

#define NUM_THREADS 5
#define ITERATIONS 100000
#define MILLISECONDS 1

static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

static _Atomic bool owned;

static void
sleep_milliseconds (int milliseconds)
{
  struct timespec ts = {
    .tv_sec = (milliseconds / 1000),
    .tv_nsec = (milliseconds % 1000) * 1000 * 1000
  };
  nanosleep (&ts, NULL);
}

static void *
worker_main (void *user_data)
{
  for (int i = 0; i < ITERATIONS; i++)
    {
      struct timespec ts;
      int ret;

      ret = clock_gettime (CLOCK_REALTIME, &ts);
      assert (ret == 0);

      ts.tv_sec += MILLISECONDS / 1000;
      ts.tv_nsec += (MILLISECONDS % 1000) * 1000 * 1000;
      if (ts.tv_nsec >= 1000 * 1000 * 1000)
        {
          ts.tv_nsec -= 1000 * 1000 * 1000;
          ts.tv_sec++;
        }

      ret = pthread_mutex_timedlock (&mutex, &ts);
      if (ret == ETIMEDOUT)
        continue;
      assert (ret == 0);

      assert (owned == false);
      owned = true;

      sleep_milliseconds (MILLISECONDS);

      assert (owned == true);
      owned = false;

      ret = pthread_mutex_unlock (&mutex);
      assert (ret == 0);
    }
}

int
main (void)
{
  pthread_t threads[NUM_THREADS];

  for (int i = 0; i < NUM_THREADS; i++)
    pthread_create (&threads[i], NULL, worker_main, NULL);

  for (int i = 0; i < NUM_THREADS; i++)
    pthread_join (threads[i], NULL);

  return 0;
}


More information about the Cygwin-developers mailing list