join

Waits for the thread associated with tid to complete.

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.

void
join

Parameters

tid 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.

Examples

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.

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();
assert(result == 42);

Meta