LIU Hao wrote:

> Yes, there's an edge case that the identifier of the dead owner gets reused 
> for the calling thread.
>
> This lock-unlock-unlock pattern looks a bit tricky. You can also prevent the 
> race condition by protecting
> the internal state with a critical section:
>
>     pthread_mutex_lock(pthread_mutex_t* m)
>     {
>       // ...
>       EnterCriticalSection(&m->state_cs);
>       WaitForSingleObject(m->mutex, timeout);
>       LeaveCriticalSection(&m->state_cs);
>     }
>
>     pthread_mutex_unlock(pthread_mutex_t* m)
>     {
>       // ...
>       EnterCriticalSection(&m->state_cs);
>       ReleaseMutex(m->mutex);
>       LeaveCriticalSection(&m->state_cs);
>     }

The issue with using critical section or any other synchronization object for 
this purpose is that if thread terminates while owning it, it will likely 
deadlock other threads trying to use this mutex. Recursive locking solves this 
issue: if thread terminates while owning (possibly recursive) lock, the next 
thread will be notified with `WaitForSingleObject` returning `WAIT_ABANDONED`.

Maybe we could use another mutex for this, but it feels like overkill to me.

- Kirill Makurin

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to