Question

there is race condition in implementation that could allow a signal to be missed. The problem is pthread_cond_wait function is not atomic? same is discussed in the comment in the file https://github.com/android/platform_bionic/blob/master/libc/bionic/pthread.c

Shouldn't this operation be atomic ?

Was it helpful?

Solution

You should not be worrying about this "race condition". As per the comment, it can only happen if one thread goes to sleep at a pathological place, stays asleep there for a long time, and other threads signal the condition variable a pathological number of times (no more, and no less).

This is actually inherent in many perfectly reasonable low-level implementations of condition variables, especially when based on futexes. Basically, after the userspace code atomically checks the value of an integer variable, it calls into the kernel to perform a blocking operation, but it instructs the kernel to abort the operation if the value of the integer variable has changed in the meantime. The pathological case is when the integer variable has wrapped all the way around its range an exact integral number of times (while the first caller stayed blocked the whole time!).

If you want to read more than you ever wanted to know about really subtle details like this, read this paper.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top