Namespaces
Variants

std::shared_mutex:: lock

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 ( ) ;
(C++17以降)

shared_mutex の排他的所有権を取得します。他のスレッドが同じ shared_mutex に対して排他ロックまたは共有ロックを保持している場合、 lock の呼び出しは、それらすべてのロックが解放されるまで実行をブロックします。 shared_mutex が排他モードでロックされている間は、他のいかなる種類のロックも保持できません。

もしスレッドが既に何らかのモード(排他または共有)で shared_mutex を所有している状態で lock が呼び出された場合、動作は未定義です。 同じミューテックスに対する前の unlock() 操作は、この操作と synchronizes-with std::memory_order で定義されている通り)関係を持ちます。

目次

パラメータ

(なし)

戻り値

(なし)

例外

エラーが発生した場合、 std::system_error をスローします。これには、 lock がその仕様を満たすのを妨げる基盤オペレーティングシステムからのエラーも含まれます。例外がスローされた場合、ミューテックスはロックされません。

注記

lock() は通常直接呼び出されません: std::unique_lock std::scoped_lock 、および std::lock_guard が排他ロックの管理に使用されます。

#include <chrono>
#include <iostream>
#include <mutex>
#include <shared_mutex>
#include <syncstream>
#include <thread>
#include <vector>
std::mutex stream_mutx;
void print(auto const& 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); // 以下の方法より優れている:
                                                                // smtx.lock();
                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
        {
            {
                // 以下の方法より優れている:
                std::shared_lock lock(smtx); // smtx.lock_shared();
                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

関連項目

ミューテックスのロックを試み、利用できない場合は制御を返す
(公開メンバ関数)
ミューテックスをアンロックする
(公開メンバ関数)
共有所有権のためにミューテックスをロックし、利用できない場合はブロックする
(公開メンバ関数)