Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call pthread_cond_broadcast with mutex held or not?

With a pthread_cond_t we have to associate a mutex, When signalling the condition I've seen code such as

pthread_mutex_lock(&mutex);

//code that makes condition true

pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mutex);

and

pthread_mutex_lock(&mutex);

//code that makes condition true

pthread_mutex_unlock(&mutex);
pthread_cond_broadcast(&cond);

Which one is the proper way ? (Does it matter ?)

like image 732
nos Avatar asked Jul 09 '09 17:07

nos


People also ask

Does pthread_cond_signal lock mutex?

The pthread_cond_signal() routine is used to signal (or wake up) another thread which is waiting on the condition variable. It should be called after mutex is locked, and must unlock mutex in order for pthread_cond_wait() routine to complete.

What happens if you dont unlock the mutex?

An errorcheck type mutex provides error checking. That is, a thread attempting to relock this mutex without first unlocking it will return with an error. The mutex is either in a locked or unlocked state for a thread. If a thread attempts to relock a mutex that it has already locked, it will return with an error.

Does pthread_cond_signal unlock?

The pthread_cond_signal() function shall unblock at least one of the threads that are blocked on the specified condition variable cond (if any threads are blocked on cond). If more than one thread is blocked on a condition variable, the scheduling policy shall determine the order in which threads are unblocked.

What does pthread_cond_wait return?

The pthread_cond_wait() routine always returns with the mutex locked and owned by the calling thread, even when returning an error. This function blocks until the condition is signaled. The function atomically releases the associated mutex lock before blocking, and atomically acquires the mutex again before returning.


1 Answers

Depends what the sinks are doing (and any other sources).

In your second code sample, it's possible that in between unlock and broadcast, a bunch of other threads will come along and do some combination of things which makes the condition false again. You'll then be broadcasting pointlessly. And you won't necessarily have the same set of waiters as were there when you changed the condition, which may or may not affect your design.

A decent sink shouldn't care if it gets woken up and the condition is false, especially if you're using broadcast. As long as every change of condition to "true" is eventually followed by a broadcast, I'm pretty sure that with appropriately-written sinks, you can broadcast a condition variable willy-nilly with or without the lock.

So I don't think it really matters, but personally I'd broadcast with the lock held, if only to avoid having to worry about it. "Atomic change-and-signal" might simplify the state diagram on your whiteboard compared with "change ... some time later, signal".

Both are proper (unlike waiting without the mutex, which isn't allowed), but I don't think it would be too difficult to come up with uses which can go wrong in the second case, that wouldn't go wrong in the first. They'd probably have to involve some waiters doing slightly unusual things, though.

The spec rather cryptically says "if predictable scheduling behavior is required, then that mutex shall be locked by the thread calling pthread_cond_broadcast() or pthread_cond_signal()."

http://www.opengroup.org/onlinepubs/009695399/functions/pthread_cond_signal.html

like image 81
Steve Jessop Avatar answered Sep 22 '22 04:09

Steve Jessop