Site icon Learn C++

What Is A Mutex Mutual Exclusion In Modern C++

What Is A Mutex Mutual Exclusion In Modern C++

CPUs and GPUs have evolved year on year to feature a greater number of cores and transistors to give more computational power for today’s servers and computers. With the advent of multiple cores, it is common now for programs to make use of multiple simultaneous threads. In modern C++, multi-thread operations are amazingly evolved since C++11, still there are new improvements in the latest standards to enhance multi-thread operations further. A mutual exclusion, or mutex, is an object that prevents multiple threads from accessing the same shared resource simultaneously.

What Is a mutex (mutual exclusion) in C++?

In modern C++, a concurrency support library is designed to solve problems that arise with multi-thread operations. This library includes built-in support for threads (std::thread), atomic operations (std::atomic), mutual exclusion (std::mutex), condition variables (std::condition_variable), and many other features.

Mutual Exclusion is a property of concurrency control. It is used when a thread of execution never enters a critical section while a concurrent thread of execution is already accessing said critical section, which refers to an interval of time during which a thread of execution accesses a shared resource or shared memory.

In programming, the Mutual Exclusion is a data exclusion method to lock and unlock data that provides use exclusive access to a resource. This is mostly needed when we use shared data in multi-thread and multi-task operations in parallel programming. In C++, we can use std::mutex to define mutex data variables to protect his shared data from being simultaneously accessed by multiple threads.

How do we use std::mutex mutual exclusion in C++?

The std::mutex is a synchronization class in <mutex> library that is used to protect shared data from being simultaneously accessed by multiple threads. std::mutex allows to use exclusive, non-recursive ownership semantics. Since C++11, here is the syntax.

[crayon-6647042f6bfe3671671908/]

Here is an example how we can use it,

[crayon-6647042f6bfec950306909/]

Is there a simple example about mutex mutual exclusion in C++?

Here is a simple example, we have a myf() function that sums area of square in a thread. Here is how we can use std::mutex here.

[crayon-6647042f6bfee521263761/]

Is there a full example of using a mutex or mutual exclusion in C++?

We can use the example myf() function above to run in many threads. Here is a full example that uses thread vector and mutex.

[crayon-6647042f6bff0986653554/]

Note that, due to thread messages of debugger, this example may run slower in debug mode but in runtime (release) mode it will be run at full speed.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Exit mobile version