The C++ standard library offers std::mutex (lock, unlock, try_lock)
functionality, that can work within a multi-threaded environment.
Also I have heard talk about wrapper libraries (e.g. Boost::mutex
) that provide, from what I can see, the same functionality (lock, unlock, try_lock
).
My question is, what is the advantage of using such wrapper libraries over the standard one?
Advantages of mutex Easy to implement: Mutexes are just simple locks that a thread obtains before entering its critical section, and then releases it. Gurantee synchronization: Since only one thread is in its critical section at any given time, there are no race conditions and data remain consistent.
You can have multiple program threads in mutex but not simultaneously. Value can be changed by any process releasing or obtaining the resource. Object lock is released only by the process, which has obtained the lock on it. Types of Semaphore are counting semaphore and binary semaphore.
The Mutex is a locking mechanism that makes sure only one thread can acquire the Mutex at a time and enter the critical section. This thread only releases the Mutex when it exits the critical section.
Wrapper libraries can be implemented using the adapter, façade, and to a lesser extent, proxy design patterns . The specific way in which a wrapper library is implemented is highly specific to the environment it is being written in and the scenarios which it intends to address.
std::mutex
, std::thread
and other elements of the threading library are only available C++11. boost::mutex
et al predate C++11. So the advantage is that you can use them if you don't have C++11 support.
While juanchopanza noted the most direct answer to the question (+1), one thing which std::mutex
introduces over the types they wrap is use of exceptions. For most people/environments/needs, that would be considered a good thing. In some cases, you may not want exception dependence. In that case/environment, the std::mutex
interfaces may not be an option or desirable.
Some wrappers, like TBB and PPL, offer far more functionality than the Standard libraries.
malloc
.std::thread
= malloc
/free
.std::vector
/std::unique_ptr
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With