View source code
Display the source code in core/sync/mutex.d from which this
page was generated on github.
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 using
local clone.
Class core.sync.mutex.Mutex
This class represents a general purpose, recursive mutex.
class Mutex
: object .Object .Monitor
;
Implemented using pthread_mutex
on Posix and CRITICAL_SECTION
on Windows.
Constructors
Name | Description |
---|---|
this
()
|
Initializes a mutex object. |
this
(obj)
|
Initializes a mutex object and sets it as the monitor for obj .
|
Methods
Name | Description |
---|---|
lock
()
|
If this lock is not already held by the caller, the lock is acquired, then the internal counter is incremented by one. |
lock_nothrow
()
|
If this lock is not already held by the caller, the lock is acquired, then the internal counter is incremented by one. |
tryLock
()
|
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. |
tryLock_nothrow
()
|
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. |
unlock
()
|
Decrements the internal lock count by one. If this brings the count to zero, the lock is released. |
unlock_nothrow
()
|
Decrements the internal lock count by one. If this brings the count to zero, the lock is released. |
factory
(classname)
|
Create instance of class specified by the fully qualified name classname. The class must either have no constructors or have a default constructor. |
opCmp
(o)
|
Compare with another Object obj. |
opEquals
(o)
|
Test whether this is equal to o .
The default implementation only compares by identity (using the is operator).
Generally, overrides for opEquals should attempt to compare objects by their contents.
|
toHash
()
|
Compute hash function for Object. |
toString
()
|
Convert Object to a human readable string. |
Example
import core .thread : Thread;
class Resource
{
Mutex mtx;
int cargo;
this() shared @safe nothrow
{
mtx = new shared Mutex();
cargo = 42;
}
void useResource() shared @safe 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();
writeln(res .cargo); // 20042
Authors
Sean Kelly
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.