Module compiler

task

Tasks are cooperative: they run on the calling thread and switch only at suspension points (IO waits, sleep, join, recv, yield_now). Every task lives inside a scope, which does not return until all of its children have finished, so no task outlives the code that started it.

Use tasks for concurrent IO; use threads for CPU-bound work. The tasks tutorial walks through the model.

Written in Coil and shipped inside the compiler.

Import what you use (use path::* is not allowed):

use task::{TaskError, Scope};

Types#

NameDescription
TaskErrorWhy a task or a scope did not produce its value.
ScopeA running scope, used to start child tasks with spawn.
TaskA handle to a child task started with Scope::spawn.
ChannelErrorWhy a channel operation failed.
ChannelA bounded queue for passing values between tasks.

Functions#

NameDescription
scopeRuns body with a Scope for starting child tasks, and returns its value once body and every child have finished.
sleepSuspends the current task for at least ms milliseconds; other tasks run meanwhile.
yield_nowLets other ready tasks run before the current one continues.
timeoutRuns body as a task and cancels it if it has not finished after ms milliseconds.
shieldRuns body so that a cancel waits for it to finish instead of interrupting it.
channelCreates a channel that holds up to capacity values.
blockingRuns work on another OS thread and waits for its result; only the current task waits, the others keep running.
blocking_withLike blocking, passing arg to work (it is copied to the other thread, like thread::spawn(work, arg)).
block_onResumes the coroutine handle until it is done and returns its completion value; values it yields are discarded.
↑ ↓ navigate ↵ open