SysTutorials

Linux Manual Pages › Section 3

stdshared_mutex(3)

std::shared_mutex - std::shared_mutex

Synopsis

Defined in header <shared_mutex> class shared_mutex; (since C++17) The shared_mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. In contrast to other mutex types which facilitate exclusive access, a shared_mutex has two levels of access:

Member types

Member type Definition native_handle_type(optional) implementation-defined

Member functions

                constructs the mutex constructor (public member function)                 destroys the mutex destructor (public member function)

operator= not copy-assignable                 (public member function) [deleted]

 Exclusive locking

                locks the mutex, blocks if the mutex is not available lock (public member function)                 tries to lock the mutex, returns if the mutex is not available try_lock (public member function)                 unlocks the mutex unlock (public member function)

Shared locking

                locks the mutex for shared ownership, blocks if the mutex is not available lock_shared (public member function)                 tries to lock the mutex for shared ownership, returns if the mutex is not available try_lock_shared (public member function)                 unlocks the mutex (shared ownership) unlock_shared (public member function)

Native handle

                returns the underlying implementation-defined native handle object native_handle (public member function)

Example

// Run this code
  // Explanation: The output below was generated on a single-core machine. When   // thread1 starts, it enters the loop for the first time and calls increment()   // followed by get(). However, before it can print the returned value to   // std::cout, the scheduler puts thread1 to sleep and wakes up thread2, which   // obviously has time enough to run all three loop iterations at once. Back to   // thread1, still in the first loop iteration, it finally prints its local copy   // of the counter’s value, which is 1, to std::cout and then runs the remaining   // two loop iterations. On a multi-core machine, none of the threads is put to   // sleep and the output is more likely to be in ascending order.

Possible output:

  123084176803584 2   123084176803584 3   123084176803584 4   123084185655040 1   123084185655040 5   123084185655040 6

See also

shared_timed_mutex provides shared mutual exclusion facility and implements locking with a timeout                    (class) (C++14)