Skip to content
FutureBlitz

    Install

    Import std.concurrent.locks

    RwLock

    std.concurrent.locks

    class

    Public Shared Class RwLock<T: Sendable>

    A value that many threads can read at once, or one can write while no other reads it. A Read gives what its Function makes of the value, and a Write gives the value what its Function makes of it, which no Read sees half done. A writer that waits goes before the readers that come after it, so it isn't kept waiting forever.

    Examples

    Const total = RwLock<Int>(0)
    total.Write(Function(n: Int) => n + 5)
    Expect(total.Read(Function(n: Int) => n * 2) == 10)
    Expect(total.Value == 5)

    std/concurrent/locks.fb, line 286

    • Public New(value: T) constructor #

      A new RwLock that holds value.

    • @PanicsAtCaller Public Method Read<R>(reader: Func(T): R): R method #

      What reader gives for the value, which no thread writes meanwhile, though others may read it. Panics when this thread reads or writes it already, in a reader or a writer, which a writer waiting between could keep waiting forever.

    • @PanicsAtCaller Public Method Write(writer: Func(T): T) method #

      Gives the value what writer gives for it, while no other thread reads or writes it. The old value goes once the Write holds no lock, so a Destroy that its going runs can use the RwLock. Panics when this thread reads or writes it already, which would wait for itself forever.

    • @PanicsAtCaller Public Property Value: T property #

      A copy of the value, as a Read gives it, or a new value, as a Write gives it.

    Report an issue with this page or with FutureBlitz on GitHub.