Namespaces
Variants

std::shared_mutex:: lock_shared

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
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
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
void lock_shared ( ) ;
(自 C++17 起)

获取互斥锁的共享所有权。如果其他线程正以独占所有权持有该互斥锁,调用 lock_shared 将阻塞执行,直到能够获得共享所有权。

如果已以任何模式(独占或共享)拥有 mutex 的线程调用 lock_shared ,则行为未定义。

如果已以共享模式锁定互斥量的共享所有者数量超过实现定义的最大值, lock_shared 将阻塞执行,直到共享所有者数量减少。最大所有者数量保证至少为10000。

同一互斥量上的先前 unlock() 操作与此操作形成 同步关系 (定义参见 std::memory_order )。

目录

参数

(无)

返回值

(无)

异常

当发生错误时抛出 std::system_error ,包括来自底层操作系统的错误,这些错误会阻止 lock 满足其规范。在任何异常被抛出的情况下,互斥锁不会被锁定。

注释

lock_shared() 通常不直接调用:使用 std::shared_lock 来管理共享锁定。

示例

#include <chrono>
#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <syncstream>
#include <thread>
#include <vector>
std::mutex stream_mutx;
void print(auto v)
{
    std::unique_lock<std::mutex> lock(stream_mutx);
    std::cout << std::this_thread::get_id() << " saw: ";
    for (auto e : v)
        std::cout << e << ' ';
    std::cout << '\n';
}
int main()
{
    using namespace std::chrono_literals;
    constexpr int N_READERS = 5;
    constexpr int LAST = -999;
    std::shared_mutex smtx;
    int product = 0;
    auto writer = [&smtx, &product](int start, int end)
    {
        for (int i = start; i < end; ++ i)
        {
            auto data = i;            
            {
                std::unique_lock<std::shared_mutex> lock(smtx);
                product = data;
            } 
            std::this_thread::sleep_for(3ms);
        }
        smtx.lock(); // 手动加锁
        product = LAST;
        smtx.unlock();
    };
    auto reader = [&smtx, &product]()
    {
        int data = 0;
        std::vector<int> seen;
        do
        {
            {
                smtx.lock_shared(); // 更推荐使用:std::shared_lock lock(smtx);
                data = product;
                smtx.unlock_shared();
            }                                   
            seen.push_back(data);
            std::this_thread::sleep_for(2ms);
        }
        while (data != LAST);
        print(seen);
    };
    std::vector<std::thread> threads;
    threads.emplace_back(writer, 1, 13);
    threads.emplace_back(writer, 42, 52);
    for (int i = 0; i < N_READERS; ++ i)
        threads.emplace_back(reader);
    for (auto&& t : threads)
        t.join();
}

可能的输出:

127755840 saw: 43 3 3 4 46 5 6 7 7 8 9 51 10 11 11 12 -999
144541248 saw: 2 44 3 4 46 5 6 7 7 8 9 51 10 11 11 12 -999
110970432 saw: 42 2 3 45 4 5 47 6 7 8 8 9 10 11 11 12 -999
119363136 saw: 42 2 3 4 46 5 6 7 7 8 9 9 10 11 11 12 12 -999
136148544 saw: 2 44 3 4 46 5 6 48 7 8 9 51 10 11 11 12 12 -999

参见

锁定互斥锁,若互斥锁不可用则阻塞
(公开成员函数)
尝试以共享模式锁定互斥锁,若互斥锁不可用则立即返回
(公开成员函数)
解锁互斥锁(共享所有权)
(公开成员函数)