英語版
このページの英語版を見る
core.sync.mutex
ミューテックス・モジュールは、相互排他的なアクセスを維持するためのプリミティブを提供する。
プリミティブを提供する。
License:
Authors:
Sean Kelly
- class
Mutex
: object.Object.Monitor; - このクラスは汎用の再帰的ミューテックスを表す。Posixではpthread_mutex 、WindowsではCRITICAL_SECTION で実装されている。Examples:
import core.thread : Thread; class Resource { Mutex mtx; int cargo; this() shared @safe nothrow { mtx = new shared Mutex(); cargo = 42; } void useResource() shared @trusted nothrow @nogc { mtx.lock_nothrow(); (cast() cargo) += 1; mtx.unlock_nothrow(); } } shared Resource res = new shared Resource(); auto otherThread = new Thread( { foreach (i; 0 .. 10000) res.useResource(); }).start(); foreach (i; 0 .. 10000) res.useResource(); otherThread.join(); assert (res.cargo == 20042);
- nothrow @nogc @trusted this();
shared nothrow @nogc @trusted this(); - ミューテックス・オブジェクトを初期化する。
- nothrow @nogc @trusted this(Object
obj
);
shared nothrow @nogc @trusted this(Objectobj
); - ミューテックス・オブジェクトを初期化し、それを
obj
.のモニターとして設定する。
obj
のモニターとして設定する。 - @trusted void
lock
();
shared @trusted voidlock
();
final nothrow @nogc @trusted voidlock_nothrow
(this Q)()
if (is(Q == Mutex) || is(Q == shared(Mutex))); - このロックが呼び出し元によってまだ保持されていない場合、ロックが取得される、 その後、内部カウンタが1つインクリメントされる。
注釈:」が必要である。 Mutex.
lock
はスローしないが、Mutexから派生したクラスはスローできる。 使用方法lock_nothrow
をnothrow @nogc 。 - @trusted void
unlock
();
shared @trusted voidunlock
();
final nothrow @nogc @trusted voidunlock_nothrow
(this Q)()
if (is(Q == Mutex) || is(Q == shared(Mutex))); - 内部ロックカウントを1つ減らす。 これでカウントがゼロになると、ロックは解放される。 カウントがゼロになると、ロックは解放される。
注釈: ロックを解除する。 Mutex.
unlock
はスローしないが、Mutexから派生したクラスはスローできる。 使用方法unlock_nothrow
nothrow @nogc を使う。 - @trusted bool
tryLock
();
shared @trusted booltryLock
();
final nothrow @nogc @trusted booltryLock_nothrow
(this Q)()
if (is(Q == Mutex) || is(Q == shared(Mutex))); - ロックが他の呼び出し元によって保持されている場合、メソッドは戻る。 そうでなければ を取得し、内部カウンタを1つインクリメントする。 カウンタが1つインクリメントされる。Returns:ロックが獲得された場合はtrue、獲得されなかった場合はfalseが返される。
注釈: ロックを取得した。 Mutex.
tryLock
はスローしないが、Mutexから派生したクラスはスローできる。 使用方法tryLock_nothrow
をnothrow @nogc 。
Copyright © 1999-2024 by the D Language Foundation
DEEPL APIにより翻訳、ところどころ修正。
このページの最新版(英語)
このページの原文(英語)
翻訳時のdmdのバージョン: 2.108.0
ドキュメントのdmdのバージョン: 2.109.1
翻訳日付 :
HTML生成日時:
編集者: dokutoku