Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/thread/shared mutex"

From cppreference.com
< cpp‎ | thread
(added links to see also)
m (Undo revision 153274 by 172.71.98.62 (talk) the story behind: https://wg21.link/N4508 - in 2014 shared_mutex was renamed to shared_timed_mutex...)
 
(7 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
{{cpp/title|shared_mutex}}
 
{{cpp/title|shared_mutex}}
 
{{cpp/thread/shared_mutex/navbar}}
 
{{cpp/thread/shared_mutex/navbar}}
{{ddcl | header=shared_mutex | since=c++17 | 1=
+
{{ddcl|header=shared_mutex|since=c++17|1=
 
class shared_mutex;
 
class shared_mutex;
 
}}
 
}}
Line 25: Line 25:
 
===Member types===
 
===Member types===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dsc | {{tt|native_handle_type}}{{mark optional}} | ''implementation-defined''}}
+
{{dsc inc|cpp/thread/dsc native_handle_type|shared_mutex}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/thread/mutex/dsc constructor | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc constructor|shared_mutex}}
{{dsc inc | cpp/thread/mutex/dsc destructor | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc destructor|shared_mutex}}
{{dsc inc | cpp/thread/mutex/dsc operator{{=}}}}
+
{{dsc inc|cpp/thread/mutex/dsc operator{{=}}}}
  
{{dsc h2 | Exclusive locking}}
+
{{dsc h2|Exclusive locking}}
{{dsc inc | cpp/thread/mutex/dsc lock | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc lock|shared_mutex}}
{{dsc inc | cpp/thread/mutex/dsc try_lock | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc try_lock|shared_mutex}}
{{dsc inc | cpp/thread/mutex/dsc unlock | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc unlock|shared_mutex}}
  
{{dsc h2 | Shared locking}}
+
{{dsc h2|Shared locking}}
{{dsc inc | cpp/thread/mutex/dsc lock_shared | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc lock_shared|shared_mutex}}
{{dsc inc | cpp/thread/mutex/dsc try_lock_shared | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc try_lock_shared|shared_mutex}}
{{dsc inc | cpp/thread/mutex/dsc unlock_shared | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc unlock_shared|shared_mutex}}
  
{{dsc h2 | Native handle}}
+
{{dsc h2|Native handle}}
{{dsc inc | cpp/thread/mutex/dsc native_handle | shared_mutex}}
+
{{dsc inc|cpp/thread/mutex/dsc native_handle|shared_mutex}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| p=true
+
|The output below was generated on a single-core machine. When {{tt|thread1}} starts, it enters the loop for the first time and calls {{tt|increment()}} followed by {{tt|get()}}. However, before it can print the returned value to {{c/core|std::cout}}, the scheduler puts {{tt|thread1}} to sleep and wakes up {{tt|thread2}}, which obviously has time enough to run all three loop iterations at once. Back to {{tt|thread1}}, still in the first loop iteration, it finally prints its local copy of the counter's value, which is {{c|1}}, to {{tt|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.
| code=
+
|code=
 
#include <iostream>
 
#include <iostream>
#include <mutex> // For std::unique_lock
+
#include <mutex>
 
#include <shared_mutex>
 
#include <shared_mutex>
 +
#include <syncstream>
 
#include <thread>
 
#include <thread>
  
class ThreadSafeCounter {
+
class ThreadSafeCounter
public:
+
{
  ThreadSafeCounter() = default;
+
public:
 +
    ThreadSafeCounter() = default;
  
  // Multiple threads/readers can read the counter's value at the same time.
+
    // Multiple threads/readers can read the counter's value at the same time.
  unsigned int get() const {
+
    unsigned int get() const
     std::shared_lock lock(mutex_);
+
     {
    return value_;
+
        std::shared_lock lock(mutex_);
  }
+
        return value_;
 +
    }
  
  // Only one thread/writer can increment/write the counter's value.
+
    // Only one thread/writer can increment/write the counter's value.
  void increment() {
+
    void increment()
     std::unique_lock lock(mutex_);
+
     {
    value_++;
+
        std::unique_lock lock(mutex_);
  }
+
        ++value_;
 +
    }
  
  // Only one thread/writer can reset/write the counter's value.
+
    // Only one thread/writer can reset/write the counter's value.
  void reset() {
+
    void reset()
     std::unique_lock lock(mutex_);
+
     {
    value_ = 0;
+
        std::unique_lock lock(mutex_);
  }
+
        value_ = 0;
 +
    }
  
private:
+
private:
  mutable std::shared_mutex mutex_;
+
    mutable std::shared_mutex mutex_;
  unsigned int value_ = 0;
+
    unsigned int value_{};
 
};
 
};
  
int main() {
+
int main()
  ThreadSafeCounter counter;
+
{
 +
    ThreadSafeCounter counter;
  
  auto increment_and_print = [&counter]() {
+
    auto increment_and_print = [&counter]()
     for (int i = 0; i < 3; i++) {
+
     {
      counter.increment();
+
        for (int i{}; i != 3; ++i)
      std::cout << std::this_thread::get_id() << ' ' << counter.get() << '\n';
+
        {
 +
            counter.increment();
 +
            std::osyncstream(std::cout)
 +
                << std::this_thread::get_id() << ' ' << counter.get() << '\n';
 +
        }
 +
    };
  
      // Note: Writing to std::cout actually needs to be synchronized as well
+
    std::thread thread1(increment_and_print);
      // by another std::mutex. This has been omitted to keep the example small.
+
    std::thread thread2(increment_and_print);
    }
+
  };
+
  
  std::thread thread1(increment_and_print);
+
    thread1.join();
  std::thread thread2(increment_and_print);
+
    thread2.join();
 
+
  thread1.join();
+
  thread2.join();
+
 
}
 
}
 
+
|p=true
// Explanation: The output below was generated on a single-core machine. When
+
|output=
// 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.
+
 
+
| output=
+
 
123084176803584 2
 
123084176803584 2
 
123084176803584 3
 
123084176803584 3
Line 126: Line 122:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/thread/dsc shared_timed_mutex}}
+
{{dsc inc|cpp/thread/dsc shared_timed_mutex}}
{{dsc inc | cpp/thread/dsc shared_lock}}
+
{{dsc inc|cpp/thread/dsc shared_lock}}
{{dsc inc | cpp/thread/dsc unique_lock}}
+
{{dsc inc|cpp/thread/dsc unique_lock}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|es|ja|ru|zh}}

Latest revision as of 05:06, 13 June 2023

 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
shared_mutex
(C++17)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
Free functions for atomic operations
Free functions for atomic flags
 
 
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:

  • shared - several threads can share ownership of the same mutex.
  • exclusive - only one thread can own the mutex.

If one thread has acquired the exclusive lock (through lock, try_lock), no other threads can acquire the lock (including the shared).

If one thread has acquired the shared lock (through lock_shared, try_lock_shared), no other thread can acquire the exclusive lock, but can acquire the shared lock.

Only when the exclusive lock has not been acquired by any thread, the shared lock can be acquired by multiple threads.

Within one thread, only one lock (shared or exclusive) can be acquired at the same time.

Shared mutexes are especially useful when shared data can be safely read by any number of threads simultaneously, but a thread may only write the same data when no other thread is reading or writing at the same time.

The shared_mutex class satisfies all requirements of SharedMutex and StandardLayoutType.

Contents

[edit] Member types

Member type Definition
native_handle_type (optional*) implementation-defined[edit]

[edit] Member functions

constructs the mutex
(public member function) [edit]
destroys the mutex
(public member function) [edit]
operator=
[deleted]
not copy-assignable
(public member function) [edit]
Exclusive locking
locks the mutex, blocks if the mutex is not available
(public member function) [edit]
tries to lock the mutex, returns if the mutex is not available
(public member function) [edit]
unlocks the mutex
(public member function) [edit]
Shared locking
locks the mutex for shared ownership, blocks if the mutex is not available
(public member function) [edit]
tries to lock the mutex for shared ownership, returns if the mutex is not available
(public member function) [edit]
unlocks the mutex (shared ownership)
(public member function) [edit]
Native handle
returns the underlying implementation-defined native handle object
(public member function) [edit]

[edit] Example

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.

#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <syncstream>
#include <thread>
 
class ThreadSafeCounter
{
public:
    ThreadSafeCounter() = default;
 
    // Multiple threads/readers can read the counter's value at the same time.
    unsigned int get() const
    {
        std::shared_lock lock(mutex_);
        return value_;
    }
 
    // Only one thread/writer can increment/write the counter's value.
    void increment()
    {
        std::unique_lock lock(mutex_);
        ++value_;
    }
 
    // Only one thread/writer can reset/write the counter's value.
    void reset()
    {
        std::unique_lock lock(mutex_);
        value_ = 0;
    }
 
private:
    mutable std::shared_mutex mutex_;
    unsigned int value_{};
};
 
int main()
{
    ThreadSafeCounter counter;
 
    auto increment_and_print = [&counter]()
    {
        for (int i{}; i != 3; ++i)
        {
            counter.increment();
            std::osyncstream(std::cout)
                << std::this_thread::get_id() << ' ' << counter.get() << '\n';
        }
    };
 
    std::thread thread1(increment_and_print);
    std::thread thread2(increment_and_print);
 
    thread1.join();
    thread2.join();
}

Possible output:

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

[edit] See also

provides shared mutual exclusion facility and implements locking with a timeout
(class) [edit]
implements movable shared mutex ownership wrapper
(class template) [edit]
implements movable mutex ownership wrapper
(class template) [edit]