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
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
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.
__gshared string received;
static void spawnedFunc(Tid ownerTid)
import std.conv: text;
// Receive a message from the owner thread.
received = text("Received the number ", i);
// Send a message back to the owner thread
// indicating success.
// Start spawnedFunc in a new thread.
auto childTid = spawn(&spawnedFunc, thisTid);
// Send the number 42 to this new thread.
// Receive the result code.
auto wasSuccessful = receiveOnly!(bool);
writeln(received); // "Received the number 42"