std.thread¶
Module entries: CondVar
, Mutex
, Semaphore
, Task
, ThreadLocal
, ThreadPool
, Waitable
- class CondVar¶
This class implements condition variables.
A condition variable allows a thread to wait until a condition is signaled. The provided mutex must be locked when
wait
is called. It will be locked when wait returns, but be unlocked for the intermediate time.- this(mutex)¶
this(this.mutex)
- wait()¶
void wait()
Wait for a thread to signal us.
- signal()¶
void signal()
Wake up (at least) one waiting thread.
- broadcast()¶
void broadcast()
Wake up all waiting threads.
- class Mutex¶
This class implements mutual exclusion.
lock
andunlock
calls must be paired. Only one thread may be betweenlock
andunlock
at the same time. To enforce this, the second thread’slock
will block until the first thread callsunlock
.Example:
auto mutex = new Mutex; assert(!mutex.isLocked); with (mutex.locked) { assert(mutex.isLocked); } assert(!mutex.isLocked);
Example:
auto mutex = new Mutex; void returnTest() { with (mutex.locked) { return; } } returnTest; assert(!mutex.isLocked);
- this()¶
this()
- lock()¶
void lock()
Lock the mutex.
- locked()¶
LockGuard locked()
Scope guard that automatically calls lock on scope entry and unlock on scope exit.
- unlocked()¶
UnlockGuard unlocked()
Scope guard that automatically calls unlock on scope entry and lock on scope exit.
Used to implement work loops that want to release their lock to do some task.
- unlock()¶
void unlock()
Unlock the mutex.
- class Semaphore¶
This class implements a counting semaphore.
acquire
will only return when a matching number ofrelease
calls has taken place.- this(i)¶
this(int i)
Initialize the class with a number of available tokens.
- acquire()¶
void acquire()
Consume a token if available; block if not.
- release()¶
void release()
Provide a token.
- class Task¶
The superclass for a task that can be scheduled to run on a thread pool.
- run()¶
void run()
- class ThreadLocal¶
This class wraps a value that is unique per thread accessing it.
- this()¶
this()
Initialize a
ThreadLocal
.
- set(value)¶
void set(T value)
Set the
ThreadLocal
to a new value.
- get()¶
T get()
Get the value stored in the
ThreadLocal
. Ifset
has not been called, the default value for the type is returned.
- class ThreadPool¶
A thread pool decouples threads and units of action. It manages multiple threads and keeps a queue of tasks, which are distributed over threads as they become available.
- this(i)¶
this(int i)
Create a thread pool with a number of threads.
- waitComplete(progress)¶
void waitComplete(void delegate!(float) progress)
Returns when all queued tasks have completed. The callback will be invoked with a number between 0 and 1, indicating progress. Intended for standalone tools that want to present a progress bar.
- addTask(task)¶
void addTask(Task task)
Add a task to the pool’s task queue.
- getTask()¶
Task getTask()
For internal use, returns a task when one has been queued.
- class Waitable¶
A convenience wrapper around CondVar that allows waiting for the outcome of an operation.
- this(value)¶
this(this.value)
Create the class with an initial value.
- set(value)¶
void set(T value)
Set the stored value to a new value.
- update(action)¶
void update(T delegate!(T) action)
Provide an action that updates the stored value.
- waitFor(condition)¶
void waitFor(bool delegate!(T) condition)
Wait for the stored value to fulfill the condition.
- waitReact(condition, react)¶
void waitReact(bool delegate!(T) condition, T delegate!(T) react)
Wait for the stored value to fulfill the condition, then modify it.