std.concurrency
This is a low-level messaging API upon which more structured or restrictive APIs may be built. The general idea is that every messageable entity is represented by a common handle type called a Tid, which allows messages to be sent to logical threads that are executing in both the current process and in external processes using the same interface. This is an important aspect of scalability because it allows the components of a program to be spread across available resources with few to no changes to the actual implementation.
A logical thread is an execution context that has its own stack and which runs asynchronously to other logical threads. These may be preemptively scheduled kernel threads, fibers (cooperative user-space threads), or some other concept with similar behavior.
The type of concurrency used when logical threads are created is determined by the Scheduler selected at initialization time. The default behavior is currently to create a new kernel thread per call to spawn, but other schedulers are available that multiplex fibers across the main thread or use some combination of the two approaches.
Copyright
Module Deinitializers 1
()Types 18
Thrown on calls to receiveOnly if a message other than the type the receiving thread expected is sent.
this(string msg = "Unexpected message type")Thrown if a message was sent to a thread via
prioritySend and the receiver does not have a handler
for a message of this type.
Variant messageThe message that was sent.Thrown when a Tid is missing, e.g. when ownerTid doesn't find an owner thread.
An opaque type used to represent a logical thread.
MessageBox mboxvoid toString(W)(ref W w) constGenerate a convenient string for identifying this `Tid`. This is only useful to see if `Tid`'s that are currently executing are the same or different, e.g. for logging and debugging. It is potent...this(MessageBox m)These behaviors may be specified when a mailbox is full.
Encapsulates all implementation-level data needed for scheduling.
When defining a Scheduler, an instance of this struct must be associated with each logical thread. It contains all implementation-level information needed by the internal API.
A Scheduler controls how threading is performed by spawn.
Implementing a Scheduler allows the concurrency mechanism used by this module to be customized according to different needs. By default, a call to spawn will create a new kernel thread that executes the supplied routine and terminates when finished. But it is possible to create Schedulers that reuse threads, that multiplex Fibers (coroutines) across a single thread, or any number of other approaches. By making the choice of Scheduler a user-level option, std.concurrency may be used for far more types of application than if this behavior were predefined.
Example:
import std.concurrency;
import std.stdio;
void main()
{
scheduler = new FiberScheduler;
scheduler.start(
{
writeln("the rest of main goes here");
});
}Some schedulers have a dispatching loop that must run if they are to work properly, so for the sake of consistency, when using a scheduler, start() must be called within main(). This yields control to the scheduler and will ensure that any spawned threads are executed in an expected manner.
An example Scheduler using kernel threads.
This is an example Scheduler that mirrors the default scheduling behavior of creating one kernel thread per call to spawn. It is fully functional and may be instantiated and used, but is not a necessary part of the default functioning of this module.
void start(void delegate() op)This simply runs op directly, since no real scheduling is needed by this approach.void spawn(void delegate() op)Creates a new kernel thread and assigns it to run the supplied op.ThreadInfo thisInfo() @property ref nothrowReturns `ThreadInfo.thisInfo`, since it is a thread-local instance of `ThreadInfo`, which is the correct behavior for this scheduler.Condition newCondition(Mutex m) nothrowCreates a new `Condition` variable. No custom behavior is needed here.An example Scheduler using Fibers.
This is an example scheduler that creates a new Fiber per call to spawn and multiplexes the execution of all fibers within the main thread.
void start(void delegate() op)This creates a new `Fiber` for the supplied op and then starts the dispatcher.void spawn(void delegate() op) nothrowThis created a new `Fiber` for the supplied op and adds it to the dispatch list.void yield() nothrowIf the caller is a scheduled `Fiber`, this yields execution to another scheduled `Fiber`.Condition newCondition(Mutex m) nothrowReturns a `Condition` analog that yields when wait or notify is called.void dispatch()InfoFiber`Fiber` which embeds a `ThreadInfo`FiberConditionUsed to determine whether a Generator is running.
A Generator is a Fiber that periodically returns values of type T to the caller via yield. This is represented as an InputRange.
T * m_valuevoid popFront()Obtains the next value from the underlying function.T moveFront()Returns the most recently generated value without executing a copy contructor. Will not compile for element types defining a postblit, because `Generator` does not return by reference.int opApply(scope int delegate(T) loopBody)int opApply(scope int delegate(size_t, T) loopBody)this(void function() fn)Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.this(void function() fn, size_t sz)Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.this(void function() fn, size_t sz, size_t guardPageSize)Initializes a generator object which is associated with a static D function. The function will be called once to prepare the range for iteration.this(void delegate() dg)Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.this(void delegate() dg, size_t sz)Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.this(void delegate() dg, size_t sz, size_t guardPageSize)Initializes a generator object which is associated with a dynamic D function. The function will be called once to prepare the range for iteration.Functions 32
bool hasLocalAliasing(Types...)()void checkops(T...)(T ops)Tid spawn(F, T...)(F fn, T args) if (isSpawnable!(F, T))Starts `fn(args)` in a new logical thread.Tid spawnLinked(F, T...)(F fn, T args) if (isSpawnable!(F, T))Starts `fn(args)` in a logical thread and will receive a `LinkTerminated` message when the operation terminates.void send(T...)(Tid tid, T vals)Places the values as a message at the back of tid's message queue.void prioritySend(T...)(Tid tid, T vals)Places the values as a message on the front of tid's message queue.receiveOnlyRet!(T) receiveOnly(T...)()Receives only messages with arguments of the specified types.bool receiveTimeout(T...)(Duration duration, T ops)Receives a message from another thread and gives up if no match arrives within a specified duration.void setMaxMailboxSize(Tid tid, size_t messages, OnCrowding doThis) @safe pureSets a maximum mailbox size.void setMaxMailboxSize(Tid tid, size_t messages, bool function(Tid) onCrowdingDoThis)Sets a maximum mailbox size.void yield() nothrowIf the caller is a `Fiber` and is not a Generator, this function will call `scheduler.yield()` or `Fiber.yield()`, as appropriate.void yield(T)(ref T value)Yields a value of type T to the caller of the currently executing generator.auto ref initOnce(alias var)(lazy typeof(var) init)Initializes var with the lazy init value in a thread-safe manner.Variables 3
Tid[string] tidByNamestring[][Tid] namesByTidScheduler schedulerSets the Scheduler behavior within the program.
This variable sets the Scheduler behavior within this program. Typically, when setting a Scheduler, scheduler.start() should be called in main. This routine will not return until program execution is complete.