std::shared_timed_mutex - cppreference.com
提供: cppreference.com
<tbody> </tbody>
shared_timed_mutex クラスは複数のスレッドによる同時アクセスから共有データを保護するために使用できる同期プリミティブです。 排他アクセスを提供する他のミューテックス型と比べて、 shared_timed_mutex は2レベルのアクセスを持ちます。
- 共有 - 複数のスレッドが同じミューテックスの所有権を共有できます。
- 排他 - 1つのスレッドだけがミューテックスを所有できます。
shared mutex は通常、読み込みスレッドは複数同時に同じリソースにデータ競合を起こさずにアクセス可能だけれども、書き込みスレッドは同時に1つしか可能でない、という状況で使われます。
timed_mutex と同様に、 shared_timed_mutex はタイムアウト付きで所有権の主張を試みる能力を try_lock_for(), try_lock_until(), try_lock_shared_for(), try_lock_shared_until() を通して提供します。
shared_timed_mutex クラスは SharedTimedMutex, StandardLayoutType の要件をすべて満たします。
メンバ関数
| ミューテックスを構築します (パブリックメンバ関数) [edit] | |
| ミューテックスを破棄します (パブリックメンバ関数) [edit] | |
operator= [削除] |
コピー代入可能ではありません (パブリックメンバ関数) [edit] |
排他ロック | |
| ミューテックスをロックします。 利用可能でない場合はブロックします (パブリックメンバ関数) [edit] | |
| ミューテックスのロックを試みます。 利用可能でない場合はリターンします (パブリックメンバ関数) [edit] | |
| ミューテックスのロックを試みます。 指定されたタイムアウト時間の間ミューテックスが利用可能にならなければリターンします (パブリックメンバ関数) [edit] | |
| ミューテックスのロックを試みます。 指定された時刻に達するまでミューテックスが利用可能にならなければリターンします (パブリックメンバ関数) [edit] | |
| ミューテックスのロックを解除します (パブリックメンバ関数) [edit] | |
共有ロック | |
| 共有所有権のためにミューテックスをロックします。 利用可能でない場合はブロックします (パブリックメンバ関数) [edit] | |
| 共有所有権のためにミューテックスのロックを試みます。 利用可能でない場合はリターンします (パブリックメンバ関数) [edit] | |
| 共有所有権のためにミューテックスのロックを試みます。 指定されたタイムアウト時間の間ミューテックスが利用可能にならなければリターンします (パブリックメンバ関数) [edit] | |
| 共有所有権のためにミューテックスのロックを試みます。 指定された時刻に達するまでミューテックスが利用可能にならなければリターンします (パブリックメンバ関数) [edit] | |
| ミューテックスの共有所有権のロックを解除します (パブリックメンバ関数) [edit] | |
例
読み込みスレッドを複数処理できるけれども書き込みスレッドは1つしか処理できないリソースを保持するクラスのためのコピー代入演算子
#include <mutex> #include <shared_mutex> class R { mutable std::shared_timed_mutex mut; /* data */ public: R& operator=(const R& other) { // requires exclusive ownership to write to *this std::unique_lock<std::shared_timed_mutex> lhs(mut, std::defer_lock); // requires shared ownership to read from other std::shared_lock<std::shared_timed_mutex> rhs(other.mut, std::defer_lock); std::lock(lhs, rhs); /* assign data */ return *this; } }; int main() { R r; }