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() 操作は、この操作と synchronizes-with 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() << " が観測: ";
    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

関連項目

ミューテックスをロックする。利用できない場合はブロックする
(公開メンバ関数)
ミューテックスの共有所有権を取得しようと試みる。利用できない場合は即座に返る
(公開メンバ関数)
ミューテックスをアンロックする(共有所有権)
(公開メンバ関数)