Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between "mutex" and "lock"?

I am very confused about the difference between a lock and mutex. In Boost docs, it says,

Lock Types

  • Class template lock_guard
  • Class template unique_lock
  • Class template shared_lock
  • Class template upgrade_lock
  • Class template upgrade_to_unique_lock
  • Mutex-specific class scoped_try_lock

Mutex Types

  • Class mutex
  • Typedef try_mutex
  • Class timed_mutex
  • Class recursive_mutex
  • Typedef recursive_try_mutex
  • Class recursive_timed_mutex
  • Class shared_mutex

In another article, I see functions like this,

boost::shared_mutex _access; void reader() {   boost::shared_lock< boost::shared_mutex > lock(_access);   // do work here, without anyone having exclusive access }     void conditional_writer() {   boost::upgrade_lock< boost::shared_mutex > lock(_access);   // do work here, without anyone having exclusive access    if (something) {     boost::upgrade_to_unique_lock< boost::shared_mutex > uniqueLock(lock);     // do work here, but now you have exclusive access   }   // do more work here, without anyone having exclusive access } 

Updated questions

  1. Can anyone offer some clarification between the "mutex" and "lock"?
  2. Is it necessary to create a shared_lock for a shared_mutex? What happen if I create a unique_lock for a shared_mutex?
  3. Or if I create a shared_lock for a mutex, does it mean the mutex can not be shared among multiple threads?
like image 415
2607 Avatar asked Feb 21 '12 17:02

2607


People also ask

What is the difference between mutex and lock?

A lock allows only one thread to enter the part that's locked and the lock is not shared with any other processes. A mutex is the same as a lock but it can be system wide (shared by multiple processes).

Is mutex a lock?

The mutual exclusion (mutex) locks or the mutex is the simplest solution. We use the mutex locks to protect the critical section and prevent the race conditions. A process needs to acquire the lock before it accesses its critical section, and it releases the lock once it finishes the execution of the critical section.

What is the difference between mutex lock and semaphore?

A Mutex is different than a semaphore as it is a locking mechanism while a semaphore is a signalling mechanism. A binary semaphore can be used as a Mutex but a Mutex can never be used as a semaphore.

When would you use a mutex lock?

Mutex: Use a mutex when you (thread) want to execute code that should not be executed by any other thread at the same time.


2 Answers

A mutex is a synchronization object. You acquire a lock on a mutex at the beginning of a section of code, and release it at the end, in order to ensure that no other thread is accessing the same data at the same time. A mutex typically has a lifetime equal to that of the data it is protecting, and that one mutex is accessed by multiple threads.

A lock object is an object that encapsulates that lock. When the object is constructed it acquires the lock on the mutex. When it is destructed the lock is released. You typically create a new lock object for every access to the shared data.

like image 156
Anthony Williams Avatar answered Sep 19 '22 23:09

Anthony Williams


A mutex is an object which can be locked. A lock is the object which maintains the lock. To create a lock, you need to pass it a mutex.

like image 26
James Kanze Avatar answered Sep 17 '22 23:09

James Kanze