在线文档教程
C++
线程支持 | Thread support

std::shared_mutex

STD::共享[医]互斥

Defined in header
class shared_mutex;(since C++17)

shared_mutex类是一个同步原语,可用于保护共享数据不被多个线程同时访问。与其他便于独占访问的互斥类型不同,共享[医]互斥锁有两个级别的访问:

  • 共享-多个线程可以共享同一个互斥体的所有权。

  • 排他性-只有一个线程可以拥有互斥体。

共享互斥通常用于多个读取器可以同时访问同一资源而不引起数据竞争的情况,但只有一个写入器可以这样做。

shared_mutex类满足SharedMutexStandardLayoutType...

成员类型

Member typeDefinition
native_handle_type(optional)implementation-defined

成员函数

(constructor)constructs the mutex (public member function)
(destructor)destroys the mutex (public member function)
operator= deletednot copy-assignable (public member function)

排他性锁定

锁锁互斥锁,如果互斥锁不可用,则阻塞%28公共成员函数%29。

试一试[医]锁试图锁定互斥锁,如果互斥锁不可用,则返回%28公共成员函数%29。

解锁解锁互斥锁%28公共成员函数%29

共享锁定

锁[医]共享锁定互斥锁以实现共享所有权,如果互斥锁不可用,则阻塞%28公共成员函数%29。

试一试[医]锁[医]Shared试图锁定互斥锁以实现共享所有权,如果互斥锁不可用,则返回%28公共成员函数%29。

解锁[医]共享解锁互斥锁%28共享所有权%29%28公共成员函数%29

本机手柄

土生土长[医]句柄返回底层实现定义的线程句柄%28公共成员函数%29

二次

#include <iostream> #include <mutex> // For std::unique_lock #include <shared_mutex> #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<std::shared_mutex> lock(mutex_ return value_; } // Only one thread/writer can increment/write the counter's value. void increment() { std::unique_lock<std::shared_mutex> lock(mutex_ value_++; } // Only one thread/writer can reset/write the counter's value. void reset() { std::unique_lock<std::shared_mutex> lock(mutex_ value_ = 0; } private: mutable std::shared_mutex mutex_; unsigned int value_ = 0; }; int main() { ThreadSafeCounter counter; auto increment_and_print = [&counter]() { for (int i = 0; i < 3; i++) { counter.increment( std::cout << std::this_thread::get_id() << ' ' << counter.get() << '\n'; // Note: Writing to std::cout actually needs to be synchronized as well // by another std::mutex. This has been omitted to keep the example small. } }; std::thread thread1(increment_and_print std::thread thread2(increment_and_print thread1.join( thread2.join( } // 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 maschine, none of the threads is put to // sleep and the output is more likely to be in ascending order.

二次

可能的产出:

二次

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

二次

另见

shared_timed_mutex (C++14)provides shared mutual exclusion facility (class)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/线程/Shared[医]互斥