join

fnvoid join(Tid tid)

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.

Parameters

tidThe Tid of the thread to join.

Throws

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.