cpp_noname
cpp_noname

Reputation: 2071

Is a thread holding the respective mutex on a spurious wakeup?

Let's take a look at the code below. Suppose a thread sees ready=false and therefore waits on the condition variable *mv_cv*, hence releasing the mutex *my_mutex* and putting itself to sleep. Some time later, something spuriously wakes the thread up while ready still holds the value false. My question is: Is the thread now holding the mutex *my_mutex* by reacquiring the mutex before waking up?

pthread_mutex_lock(&my_mutex);

while ( !ready )
{
        pthread_cond_wait(&my_cv, &my_mutex);
}

//some operation goes here

pthread_mutex_unlock(&my_mutex);

Upvotes: 2

Views: 144

Answers (1)

yohjp
yohjp

Reputation: 3125

Yes. Spurious wake-up is one kind of successful return, and post condition (reacquiring lock of the mutex) will be fulfilled.

Upvotes: 3

Related Questions