Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of other mutex wrapper libraries over std::mutex?

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?

like image 225
Kam Avatar asked Aug 26 '12 14:08

Kam


People also ask

What are the advantages of mutex in C++?

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.

Can We have multiple program threads in mutex?

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.

What is a mutex in Java?

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.

How are wrapper libraries implemented?

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.


3 Answers

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.

like image 87
juanchopanza Avatar answered Nov 07 '22 06:11

juanchopanza


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.

like image 43
justin Avatar answered Nov 07 '22 06:11

justin


Some wrappers, like TBB and PPL, offer far more functionality than the Standard libraries.

  1. Using pthreads/CreateThread yourself = writing your own malloc.
  2. Using std::thread = malloc/free.
  3. Using TBB/PPL = std::vector/std::unique_ptr.
like image 25
Puppy Avatar answered Nov 07 '22 06:11

Puppy