C++C++11C++14C++17C++20IteratorsLearn C++Syntax

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.

Here is an example how we can use it,

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.

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.

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.

What Is A Mutex Mutual Exclusion In Modern C++ C++ Builder logo

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++17C++20Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

C++C++17C++20Learn C++NumericsSyntax

How To Compute The Greatest Common Divisor And Least Common Multiple in C++?

C++C++17Language FeatureLearn C++

How To Use Skia Shader SkSL Shading Language Code in C++ Builder?

Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++