std::mutex

A mutex is an object that is used to guard a shared resource to ensure the use of the shared resource does not result in corruption. To accomplish this, std::mutex has a lock() function and an unlock() function. The lock function acquires access to a shared resource (sometimes referred to as a critical section). unlock() releases this previously acquired access. Any attempt to execute the lock() function after another thread has already executed lock() will result in the thread having to wait until the unlock() function is executed.

How std::mutex is implemented depends on the CPU's architecture and the operating system; however, in general, a mutex can be implemented with a simple integer. If the integer is 0, the lock() function ...

Get Advanced C++ Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.