LEARN VOID / CONCURRENCY

Task groups & cancellation

Give related work a lifetime.

Async execution

This example uses the explicit async host adapter. Its source is checked by the compiler; it is not a synchronous CLI entry program.

task-groups.voidDownload source ↓
struct progress:
    completed: i32 = 0

async func record_work(report: progress, amount: i32):
    report.completed += amount

async func entry(amount: i32) -> i32:
    var report = progress()
    with tasks.group() as jobs:
        var first = record_work(report, amount)
        var second = record_work(report, amount + 1)
    return report.completed
Input 5Expected result 11

Schedule related work together

Both record_work calls belong to the explicit task group. They update the same progress report on the cooperative async executor. The returned handles name the scheduled work; this example does not need to await them individually.

Leave the scope with completed work

Normal group exit waits for its children. The final return is outside the group, so both updates have completed before the total is read. With input 5, the two amounts are 5 and 6, returning 11.

Keep early-exit behavior explicit

An early exit from a nested scope requests cancellation of unfinished work and waits for it to settle. Cancellation is cooperative; dropping a handle does not detach a task. This focused example shows the normal completion path and requires the explicit async host adapter.