Skip to main content

Module mutex

Module mutex 

Source
Expand description

This is a copy-pasta of std.Thread.Mutex with some changes.

  • No assert with unreachable
  • uses bun.Futex instead of std.Thread.Futex Synchronized with std as of Zig 0.14.1

Mutex is a synchronization primitive which enforces atomic access to a shared region of code known as the “critical section”. It does this by blocking ensuring only one thread is in the critical section at any given point in time by blocking the others. Mutex can be statically initialized and is at most size_of::<u64>() large. Use lock() or try_lock() to enter the critical section and unlock() to leave it.

Example:

ⓘ
let m = Mutex::default();

{
    m.lock();
    // ... critical section code
    m.unlock();
}

if m.try_lock() {
    // ... critical section code
    m.unlock();
}

Structs§

FutexImpl
Mutex
MutexGuard
RAII guard returned by Mutex::lock_guard. Unlocks on Drop.

Type Aliases§

ReleaseImpl