View source code
Display the source code in std/concurrency.d from which thispage was generated on github.
Report a bug
If you spot a problem with this page, click here to create aGitHub 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 usinglocal clone.

Function std.concurrency.join

Waits for the thread associated with tid to complete.

void join(
  Tid tid
);

This function blocks until the thread represented by tid finishes executing and then releases all OS resources associated with it (thread stack, thread-local storage, etc.). This is essential for preventing resource leaks in long-running applications that create many short-lived threads.

For threads created by spawn, this function must be called to properly free OS resources. Without calling join, thread stacks (~8 MB each on typical systems) will accumulate in virtual memory for the lifetime of the process.

Parameters

NameDescription
tid The Tid of the thread to join.

Throws

core.thread.osthread.ThreadError if the thread has already been joined or if the thread was created by a custom Scheduler.

Example

auto tid = spawn(&someFunction);
// ... do other work ...
join(tid); // Wait for thread to complete and free resources

Note

It is an error to call join on the same Tid more than once. The function will throw if the thread was created by a Scheduler rather than directly as a system thread.

Example

import core.time : msecs;
import core.thread : Thread;

auto tid = spawn((int x) {
    Thread.sleep(10.msecs);
    ownerTid.send(x * 2);
}, 21);

join(tid); // Wait for thread to finish
auto result = receiveOnly!int();
writeln(result); // 42

Authors

Sean Kelly, Alex Rønne Petersen, Martin Nowak

License

Boost License 1.0.