del bao
del bao

Reputation: 1174

what is the decisive difference between mutex and condition variables?

I read several posts (e.g., here) on stackoverflow about the difference but none of them give a clear explanation.

Wonder what's the scenario that requires the use of the one instead of the other and why?

Upvotes: 0

Views: 92

Answers (1)

del bao
del bao

Reputation: 1174

After digging into the concept, I realize the decisive difference.

A mutex provides mutual exclusive semantics, which a thread blocks on another thread which is in a critical section. On the other hand, a conditional variable doesn't require another thread. It's a condition that a thread blocks on.

Take a producer/consumer queue for example. If the queue is empty, a consumer will block on this condition instead of on another producer/consumer thread.

Upvotes: 1

Related Questions