Lyricdev
GitHub

std.sync

Synchronization between tasks and threads: Atomic, Mutex, RwLock, Once and Semaphore.

The lock state lives in std.task, whose scheduler releases the locks of a task that panicked. A task that waits for a lock or a permit is parked while the other tasks of its thread run.

Examples

import std.io { println };
import std.sync { Atomic };
import std.task { spawn };

fn main(): void throws Error {
    let hits = Atomic<int>.new(0);
    let tasks = [spawn(() => { hits.fetchAndAdd(1); }), spawn(() => { hits.fetchAndAdd(2); })];
    for (t in tasks) {
        try t.await();
    }
    println(hits.load());
}
3

Types

AtomicValue interface — The types an Atomic holds: those the machine reads and writes in one step.
Atomic class — A value that every thread reads and writes in one step.
Mutex class — A value that one task at a time holds.
RwLock class — A value that readers share and a writer holds alone.
Once class — Work done once.
Semaphore class — A count of permits that tasks take and give back.

Extensions

extend int :: [AtomicValue]

Conforms to AtomicValue.

extend bool :: [AtomicValue]

Conforms to AtomicValue.