Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Conditional variables, Mutexes and Locks

For example the c++0x interfaces

I am having a hard time figuring out when to use which of these things (cv, mutex and lock). Can anyone please explain or point to a resource?

Thanks in advance.

like image 636
Ronny Brendel Avatar asked Jun 28 '09 18:06

Ronny Brendel


People also ask

What's the difference between mutex and condition variable?

While mutex implement synchronization by controlling thread access to data, condition variables allow threads to synchronize based upon the actual value of data. Without condition variables, the programmer would need to have threads continually polling (possibly in a critical section), to check if the condition is met.

What is a lock and condition variable?

A collection of procedures manipulating a shared data structure. One lock that must be held whenever accessing the shared data (typically each procedure acquires the lock at the very beginning and releases the lock before returning). One or more condition variables used for waiting.

What is the difference between Semaphore and condition variable?

In most systems, boolean semaphores are just a special case of counting semaphores, also known as general semaphores. The condition variable is a synchronization primative that provides a queue for threads waiting for a resource. A thread tests to see if the resource is available. If it is available, it uses it.

Why do we need to use conditional variables in Pthread with a mutex lock?

It's just the way that condition variables are (or were originally) implemented. The mutex is used to protect the condition variable itself. That's why you need it locked before you do a wait. The wait will "atomically" unlock the mutex, allowing others access to the condition variable (for signalling).


1 Answers

On the page you refer to, "mutex" is the actual low-level synchronizing primitive. You can take a mutex and then release it, and only one thread can take it at any single time (hence it is a synchronizing primitive). A recursive mutex is one which can be taken by the same thread multiple times, and then it needs to be released as many times by the same thread before others can take it.

A "lock" here is just a C++ wrapper class that takes a mutex in its constructor and releases it at the destructor. It is useful for establishing synchronizing for C++ scopes.

A condition variable is a more advanced / high-level form of synchronizing primitive which combines a lock with a "signaling" mechanism. It is used when threads need to wait for a resource to become available. A thread can "wait" on a CV and then the resource producer can "signal" the variable, in which case the threads who wait for the CV get notified and can continue execution. A mutex is combined with CV to avoid the race condition where a thread starts to wait on a CV at the same time another thread wants to signal it; then it is not controllable whether the signal is delivered or gets lost.

like image 172
Antti Huima Avatar answered Oct 08 '22 09:10

Antti Huima