sync - Concurrency Primitives¶
The sync module provides thread-safe concurrency primitives for multi-threaded programs.
Import¶
Types¶
Mutex<T>¶
Thread-safe wrapper protecting a value of any type.
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.
Examples¶
Basic Mutex¶
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¶
- Mutex Tutorial - Detailed guide
- Channels - Message passing
- TaskGroup - Structured concurrency