Skip to content
CryoCryo home
Stdlibsync

rwlock

import std::sync::rwlock; · source

RwLock<T, A>

type struct RwLock<T, A = GlobalAlloc> {
    inner: RwLockInner<T, A>*;

    static new(value: T) -> RwLock<T, GlobalAlloc>;
    static new_in(value: T, alloc: A) -> RwLock<T, A>;
    static try_new(value: T) -> Result<RwLock<T, GlobalAlloc>, AllocError>;
    static try_new_in(value: T, alloc: A) -> Result<RwLock<T, A>, AllocError>;
    read(&this) -> RwLockReadGuard<T, A>;
    try_read(&this) -> Option<RwLockReadGuard<T, A>>;
    write(&this) -> RwLockWriteGuard<T, A>;
    try_write(&this) -> Option<RwLockWriteGuard<T, A>>;
}

Many concurrent readers or one exclusive writer. Reach for it over a Mutex when the value is read often and written rarely, since the read path does not serialize. Multiple read guards may coexist; a write guard excludes everything else.

The guards

type struct RwLockReadGuard<T, A> {
    inner: RwLockInner<T, A>*;

    as_ptr(&this) -> T*;
}
type struct RwLockWriteGuard<T, A> {
    inner: RwLockInner<T, A>*;

    as_ptr(&this) -> T*;
}

Both dereference through as_ptr() and release the lock when dropped. Multiple read guards may coexist; a write guard excludes everything else.

Trait implementations

implement<T, A> trait Drop for struct RwLock<T, A>
where T: Drop, A: Allocator

implement<T, A> trait Drop for struct RwLockReadGuard<T, A>

implement<T, A> trait Drop for struct RwLockWriteGuard<T, A>