Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

core.sync.mutex

The mutex module provides a primitive for maintaining mutually exclusive access.
License:
Boost License 1.0
Authors:
Sean Kelly

Sourcecore/sync/mutex.d

classMutex:object.Object.Monitor;
This class represents a general purpose, recursive mutex.
Implemented usingpthread_mutex on Posix andCRITICAL_SECTION on Windows.
Examples:
import core.thread : Thread;class Resource{Mutex mtx;int cargo;this()shared @safenothrow    {        mtx =newsharedMutex();        cargo = 42;    }void useResource()shared @trustednothrow @nogc    {        mtx.lock_nothrow();        (cast() cargo) += 1;        mtx.unlock_nothrow();    }}shared Resource res =newshared 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();

nothrow @nogc @trusted this() shared;
Initializes a mutex object.
nothrow @nogc @trusted this(Objectobj);

nothrow @nogc @trusted this(Objectobj) shared;
Initializes a mutex object and sets it as the monitor forobj.

Inobj must not already have a monitor.

@trusted voidlock();

@trusted voidlock() shared;

final nothrow @nogc @trusted voidlock_nothrow(this Q)()
if (is(Q == Mutex) || is(Q == shared(Mutex)));
If this lock is not already held by the caller, the lock is acquired, then the internal counter is incremented by one.

NoteMutex.lock does not throw, but a class derived from Mutex can throw. Uselock_nothrow innothrow @nogc code.

@trusted voidunlock();

@trusted voidunlock() shared;

final nothrow @nogc @trusted voidunlock_nothrow(this Q)()
if (is(Q == Mutex) || is(Q == shared(Mutex)));
Decrements the internal lock count by one. If this brings the count to zero, the lock is released.

NoteMutex.unlock does not throw, but a class derived from Mutex can throw. Useunlock_nothrow innothrow @nogc code.

@trusted booltryLock();

@trusted booltryLock() shared;

final nothrow @nogc @trusted booltryLock_nothrow(this Q)()
if (is(Q == Mutex) || is(Q == shared(Mutex)));
If the lock is held by another caller, the method returns. Otherwise, the lock is acquired if it is not already held, and then the internal counter is incremented by one.
Returns:
true if the lock was acquired and false if not.

NoteMutex.tryLock does not throw, but a class derived from Mutex can throw. UsetryLock_nothrow innothrow @nogc code.

Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 00:05:34 2026

[8]ページ先頭

©2009-2026 Movatter.jp