<shared_mutex>

<shared_mutex> ヘッダーで、複数のスレッドがアクセスできる共有のデータの保護のための同期プリミティブが提供されます。 ミューテックス クラスで提供される排他アクセス制御だけでなく、共有ミューテックス クラスによって非排他的なアクセス用の複数のスレッドによる共有所有権も可能になります。 共有ミューテックスを使用して、競合状態を発生させず複数のスレッドで読み取ることができるリソースを制御できますが、共有ミューテックスは 1 つのスレッドによって排他的に書き込まれる必要があります。

ヘッダー <shared_mutex> では、共有ミューテックスのサポートのために、クラス shared_mutexshared_timed_mutex、クラス テンプレート shared_lock、およびテンプレート関数 swap を定義します。

クラス 説明
shared_mutex クラス 1 つのエージェントによって排他的にロックされるか、または複数のエージェントで非排他的に共有される共有 mutex 型。
shared_timed_mutex クラス 1 つのエージェントによって排他的にロックされるか、または複数のエージェントで非排他的に共有される共有 timed mutex 型。
shared_lock クラス 時間指定のロック操作と複数のエージェントによる非排他的な共有をサポートするために共有ミューテックスをラップするクラス テンプレート。
関数 説明
スワップ 関数のパラメーターによって参照される共有ミューテックス オブジェクトの内容を交換します。

構文

namespace std {
    class shared_mutex;
    class shared_timed_mutex;
    template <class Mutex>
class shared_lock;
    template <class Mutex>
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;
}

解説

クラス shared_mutex のインスタンスは、共有 mutex 型、つまり、スコープ内でのミューテックスの共有所有権を制御する型です。 共有 mutex 型は、mutex 型および共有の非排他的な所有権をサポートするためのメンバーの要件をすべて満たしています。

共有 mutex 型は、追加のメソッド lock_sharedunlock_shared、および try_lock_shared をサポートします。

  • lock_shared メソッドは、スレッドがミューテックスの共有所有権を取得するまでそのスレッドの呼び出しをブロックします。

  • unlock_shared メソッドは、スレッドの呼び出しによって保持されるミューテックスの共有所有権を解放します。

  • try_lock_shared メソッドは、ミューテックスをブロックせずに共有所有権を取得しようとします。 その戻り値の型は、bool に変換でき、メソッドが所有権を得ると true になりますが、それ以外の場合は false です。

クラス shared_timed_mutex は、共有 timed mutex 型、つまり、共有 mutex 型と timed mutex 型の両方の要件を満たす型です。

共有 timed mutex 型は、追加のメソッド try_lock_shared_for および try_lock_shared_until をサポートします。

  • try_lock_shared_for メソッドは、パラメーターで指定した期間が経過するまで、ミューテックスの共有所有権を取得しようとします。 この期間が正ではない場合、このメソッドは try_lock_shared と等しくなります。 このメソッドは、共有所有権を取得しない限り、指定した期間内では返しません。 その戻り値は、メソッドが所有権を取得した場合は true、それ以外の場合は false です。

  • try_lock_shared_until メソッドは、指定された絶対時間が経過するまで、ミューテックスの共有所有権を取得しようとします。 指定した時間が既に経過している場合、このメソッドは try_lock_shared と等しくなります。 このメソッドは、共有所有権を取得しない限り、指定した時間の前に返しません。 その戻り値は、メソッドが所有権を取得した場合は true、それ以外の場合は false です。

shared_lock クラス テンプレートは時間指定のロックのサポートおよび共有ミューテックスへの所有権の転送のサポートを拡張します。 ミューテックスの所有権は構築時または構築後に取得でき、別の shared_lock オブジェクトに転送できます。 shared_lock 型のオブジェクトは移動できますが、コピーできません。

警告

Visual Studio 2015 以降の C++ 標準ライブラリ同期型は、Windows 同期プリミティブに基づいており、ターゲット プラットフォームが Windows XP の場合を除き、ConcRT を使用しなくなりました。 <shared_mutex> で定義されている型は、ConcRT 型または関数には使用しないでください。

クラス

shared_mutex クラス

クラス shared_mutex は、共有所有権セマンティクスで非再帰的なミューテックスを実装します。

class shared_mutex {
public:
   shared_mutex();
   ~shared_mutex();
   shared_mutex(const shared_mutex&) = delete;
   shared_mutex& operator=(const shared_mutex&) = delete;
   // Exclusive ownership
   void lock();
   // blocking
   bool try_lock();
   void unlock();
   // Shared ownership
   void lock_shared();
   // blocking
   bool try_lock_shared();
   void unlock_shared();
   // Getters
   typedef void** native_handle_type; // implementation defined
   native_handle_type native_handle();
   };

shared_timed_mutex クラス

クラス shared_timed_mutex は、timed mutex 型の要件を満たしている共有所有権セマンティクスで非再帰的なミューテックスを実装します。

class shared_timed_mutex {
public:
   shared_timed_mutex();
   ~shared_timed_mutex();
   shared_timed_mutex(const shared_timed_mutex&) = delete;
   shared_timed_mutex& operator=(const shared_timed_mutex&) = delete;
   // Exclusive ownership
   void lock();
   // blocking
   bool try_lock();
   template <class Rep, class Period>
   bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
   template <class Clock, class Duration>
   bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
   void unlock();
   // Shared ownership
   void lock_shared();
   // blocking
   bool try_lock_shared();
   template <class Rep, class Period>
   bool try_lock_shared_for(const chrono::duration<Rep, Period>& rel_time);
   template <class Clock, class Duration>
   bool try_lock_shared_until(const chrono::time_point<Clock, Duration>& abs_time);
   void unlock_shared();
   };

shared_lock クラス

クラス テンプレート shared_lock は、スコープ内の共有ミューテックス オブジェクトの共有所有権を制御します。 テンプレート パラメーターは、共有 mutex 型である必要があります。

class shared_lock {
public:
   typedef Mutex mutex_type;
   shared_lock() noexcept;
   explicit shared_lock(mutex_type& m);
   // blocking
   shared_lock(mutex_type& m, defer_lock_t) noexcept;
   shared_lock(mutex_type& m, try_to_lock_t);
   shared_lock(mutex_type& m, adopt_lock_t);
   template <class Clock, class Duration>
   shared_lock(mutex_type& m,
   const chrono::time_point<Clock, Duration>& abs_time);
   template <class Rep, class Period>
   shared_lock(mutex_type& m,
   const chrono::duration<Rep, Period>& rel_time);
   ~shared_lock();
   shared_lock(shared_lock const&) = delete;
   shared_lock& operator=(shared_lock const&) = delete;
   shared_lock(shared_lock&& u) noexcept;
   shared_lock& operator=(shared_lock&& u) noexcept;
   void lock();
   // blocking
   bool try_lock();
   template <class Rep, class Period>
   bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
   template <class Clock, class Duration>
   bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
   void unlock();
   // Setters
   void swap(shared_lock& u) noexcept;
   mutex_type* release() noexcept;
   // Getters
   bool owns_lock() const noexcept;
   explicit operator bool () const noexcept;
   mutex_type* mutex() const noexcept;
private:
   mutex_type* pm; // exposition only
   bool owns; // exposition only
   };

関数

スワップ

shared_lock オブジェクトを交換します。

template <class Mutex>
void swap(shared_lock<Mutex>& x, shared_lock<Mutex>& y) noexcept;

2 つの shared_lock オブジェクトの内容を交換します。 実質的に x.swap(y) と同じです。

必要条件

ヘッダー:<shared_mutex>

名前空間: std

関連項目

ヘッダー ファイル リファレンス
<mutex>