View source code
Display the source code in std/concurrency.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.

Function std.concurrency.spawn

Starts fn(args) in a new logical thread.

Tid spawn(F, T...) (
  F fn,
  T args
if (isSpawnable!(F, T));

Executes the supplied function in a new logical thread represented by Tid. The calling thread is designated as the owner of the new thread. When the owner thread terminates an OwnerTerminated message will be sent to the new thread, causing an OwnerTerminated exception to be thrown on receive().


fn The function to execute.
args Arguments to the function.


A Tid representing the new logical thread.


args must not have unshared aliasing. In other words, all arguments to fn must either be shared or immutable or have no pointer indirection. This is necessary for enforcing isolation among threads.

Similarly, if fn is a delegate, it must not have unshared aliases, meaning fn must be either shared or immutable.


static void f(string msg)
    writeln(msg); // "Hello World"

auto tid = spawn(&f, "Hello World");



char[] has mutable aliasing.

string msg = "Hello, World!";

static void f1(string msg) {}
static assert(!__traits(compiles, spawn(&f1, msg.dup)));
static assert( __traits(compiles, spawn(&f1, msg.idup)));

static void f2(char[] msg) {}
static assert(!__traits(compiles, spawn(&f2, msg.dup)));
static assert(!__traits(compiles, spawn(&f2, msg.idup)));


New thread with anonymous function

    ownerTid.send("This is so great!");
writeln(receiveOnly!string); // "This is so great!"


Sean Kelly, Alex Rønne Petersen, Martin Nowak


Boost License 1.0.