YouTip LogoYouTip

Cpp Libs Mutex

In multi-threaded programming, ensuring data consistency and thread safety is crucial.

The C++ standard library provides a rich set of synchronization primitives for controlling access to shared resources.

The <mutex> header file in the C++ standard library provides a set of tools for implementing synchronization and mutual exclusion between threads in multi-threaded programs.

The <mutex> header file was introduced in C++11 and contains classes and functions for mutexes (mutual exclusion locks). A mutex is a synchronization mechanism that prevents multiple threads from accessing shared resources simultaneously.

A Mutex is a synchronization primitive used to control access to shared resources. When a thread needs to access a shared resource, it attempts to lock the mutex. If the mutex is already locked by another thread, the requesting thread will be blocked until the mutex is released.

Basic Syntax

In C++, the <mutex> header file provides the following main classes:

  • std::mutex: Basic mutex.
  • std::recursive_mutex: Recursive mutex, allowing the same thread to lock multiple times.
  • std::timed_mutex: Mutex with timeout functionality.
  • std::recursive_timed_mutex: Recursive mutex with timeout functionality.

Examples

1. Using std::mutex

Below is a simple example demonstrating how to use std::mutex in C++ to synchronize access to shared resources.

Example


#include 

#include 

#include 

std::mutex mtx;// Global mutex

int shared_resource =0;

void increment(){

for(int i =0; i <10000;++i){

 mtx.lock();// Lock the mutex

++shared_resource;

 mtx.unlock();// Unlock the mutex

}

}

int main(){

 std::thread t1(increment);

 std::thread t2(increment);

t1.join();

 t2.join();

std::cout<<"Final value of shared_resource: "<< shared_resource << std::endl;

return 0;

}

Output:

Final value of shared_resource: 20000

2. Using std::recursive_mutex

Recursive mutex allows the same thread to lock the same mutex multiple times. Below is an example using std::recursive_mutex.

Example


#include 

#include 

#include 

std::recursive_mutex rmtx;// Create a recursive mutex object

int shared_resource =0;// Shared resource

// Recursive function

void recursive_increment(int count){

if(count <=0)return;

std::lock_guard lock(rmtx);// Lock, ensuring thread safety

++shared_resource;

 std::cout<<"Incremented shared_resource to "<< shared_resource <<" (count = "<< count <<")"<< std::endl;

// Recursive call

 recursive_increment(count -1);

}

int main(){

 std::thread t1(recursive_increment, 3);// Thread t1 executes recursive_increment(3)

 std::thread t2(recursive_increment, 3);// Thread t2 executes recursive_increment(3)

t1.join();// Wait for thread t1 to complete

 t2.join();// Wait for thread t2 to complete

std::cout<<"Final value of shared_resource: "<< shared_resource << std::endl;

return 0;

}

Output:


Incremented shared_resource to 1 (count = 3)
Incremented shared_resource to 2 (count = 2)
Incremented shared_resource to 3 (count = 1)
Incremented shared_resource to 4 (count = 0)
Incremented shared_resource to 5 (count = 3)
Incremented shared_resource to 6 (count = 2)
Incremented shared_resource to 7 (count = 1)
Incremented shared_resource to 8 (count = 0)
Final value of shared_resource: 8

<

← Cpp Libs FutureCpp Libs Chrono β†’