Back to course

Atomics and Mutexes

Full Course: Zig Programming From Zero to Hero

Synchronization

To safely share data between threads, Zig provides atomics and mutexes.

zig var mutex = std.Thread.Mutex{};

{ mutex.lock(); defer mutex.unlock(); // Safe code here }

Atomics allow lock-free synchronization for primitive types using @atomicRmw and @atomicLoad.