mutex Thread Synchronization (C++)

std::mutex (C++)

The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.

Mutex

Mutex as the name hints implies mutual exclusion. A mutex is used to guard shared data such as a linked-list, an array or any simple primitive type. A mutex allows only a single thread to access a resource.

First saw this with the Singleton pattern.

I am seeing this in NVIDIA codebase.

Resources

Locking functions:

  • lock: locks the mutex, blocks if the mutex is not available, see lock (C++)
  • try_lock: tries to lock the mutex, returns if the mutex is not available
  • unlock: unlocks the mutex

You need to include the <mutex> library to use this functionality

#include <mutex>          // std::mutex

An example

void print_block (int n, char c) {
  // critical section (exclusive access to std::cout signaled by locking mtx):
  mtx.lock();
  for (int i=0; i<n; ++i) { std::cout << c; }
  std::cout << '\n';
  mtx.unlock();
}
 
int main ()
{
  std::thread th1 (print_block,50,'*');
  std::thread th2 (print_block,50,'$');
 
  th1.join();
  th2.join();
 
  return 0;
}

Use a Lock

It is best practice to wrap these mutexes in a lock (C++) which employ RAII, and ensures the mutexes are unlocked when exiting.

Some more advanced mutexes in C++:

  • shared_time_mutex

shared_time_mutex

It’s shared_mutex + timed_mutex

shared_mutex: https://en.cppreference.com/w/cpp/thread/shared_mutex timed_mutex: https://en.cppreference.com/w/cpp/thread/timed_mutex

The shared_timed_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. In contrast to other mutex types which facilitate exclusive access, a shared_timed_mutex has two levels of access:

  • exclusive - only one thread can own the mutex.
  • shared - several threads can share ownership of the same mutex.

Shared mutexes are usually used in situations when multiple readers can access the same resource at the same time without causing data races, but only one writer can do so.

https://en.cppreference.com/w/cpp/thread/shared_timed_mutex

Also look at this