Skip to content

sync - Concurrency Primitives

The sync module provides thread-safe concurrency primitives for multi-threaded programs.

Import

import sync

Types

Mutex<T>

Thread-safe wrapper protecting a value of any type.

let m = sync.Mutex(42)     # Mutex<int>
let s = sync.Mutex("hi")   # Mutex<str>

Methods:

Method Returns Description
lock() MutexGuard<T> Acquire lock (blocking)
try_lock() Option<MutexGuard<T>> Try lock (non-blocking)

MutexGuard<T>

RAII guard for safe access to mutex-protected values.

let guard = mutex.lock()
print(guard.value)  # Access the protected value

Examples

Basic Mutex

import sync

let counter = sync.Mutex(0)
let guard = counter.lock()
print(guard.value)  # 0

With Structs

import sync

struct Point:
    x: int
    y: int

let p = sync.Mutex(Point(x=10, y=20))
let guard = p.lock()
print(guard.value.x)  # 10

See Also