Skip to content

Sync

Module: std.sync
Source: std/sync.zn

Usage Example

use std.sync;
fn main() {
let counter = Arc.new(Mutex.new(0));
{
let guard = counter.get_mut().lock();
*guard += 1;
};
@println("count: {}", *counter.get_mut().lock());
}

Mutex[T]

Mutual exclusion lock protecting shared data


Fields:

os: OsMutex,
data: T,
open: bool,

Constructors:

pub fn new(value: T) Self;

Methods:

pub fn lock(*self) MutexGuard[T];
pub fn try_lock(*self) Option[MutexGuard[T]];
pub fn into_inner(self) T;

Interfaces:

  • core.ops : Drop

MutexGuard[T]

Held mutex, bind in scope


Fields:

lock: *OsMutex,
got: *T,

Interfaces:

  • core.ops : Deref[T: Copy], DerefPtr, Drop

RwLock[T]

Shared read, exclusive write lock


Fields:

os: OsRwLock,
data: T,
open: bool,

Constructors:

pub fn new(value: T) Self;

Methods:

pub fn read(*self) RwReadGuard[T];
pub fn try_read(*self) Option[RwReadGuard[T]];
pub fn write(*self) RwWriteGuard[T];
pub fn try_write(*self) Option[RwWriteGuard[T]];
pub fn into_inner(self) T;

Interfaces:

  • core.ops : Drop

RwReadGuard[T]

Held shared read, bind in scope


Fields:

lock: *OsRwLock,
got: *T,

Interfaces:

  • core.ops : Deref[T: Copy], DerefPtr, Drop

RwWriteGuard[T]

Held exclusive write, bind in scope


Fields:

lock: *OsRwLock,
got: *T,

Interfaces:

  • core.ops : Deref[T: Copy], DerefPtr, Drop

Arc[T]

Atomic reference count shared ownership, thread safe version of Shared


Fields:

ptr: *T,
count: *i64,

Constructors:

pub fn new(value: T) Self;

Methods:

pub fn use_count(*const self) i64;
pub fn is_unique(*const self) bool;
pub fn get(*const self) *const T;
pub fn get_mut(*self) *T;

Interfaces:

  • core.ops : Clone, Drop, Deref[T: Copy], DerefPtr
  • core.io : Display[T: Display], Debug[T: Debug]