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.
Table of Contents
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.
1 2 3 |
mutex <variable_name>; |
Here is an example how we can use it,
1 2 3 4 5 6 7 |
std::mutex mtx; mtx.lock(); // do operations mtx.unlock(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int sum = 0; std::mutex sum_mutex; void myf(int r) { int tmp = r*r; sum_mutex.lock(); sum += tmp; sum_mutex.unlock(); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#include <iostream> #include <vector> #include <thread> #include <mutex> int sum = 0; std::mutex sum_mutex; void myf(int r) { int tmp = M_PI*r*r; sum_mutex.lock(); sum += tmp; sum_mutex.unlock(); } int main() { std::vector< std::thread> myt; for (unsigned int i = 0; i < 1000; i++) { myt.push_back( std::thread( &myf, i) ); // add functions to thread vector } for (auto& t : myt) { t.join(); // join threads and run } std:: cout << "Sum :" << sum << std::endl; system("pause"); return 0; } |
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.