Function in thread compiler

with_write

fn with_write<A, B>(RwLock lock, A -> (A, B) f) -> Result<B, ThreadError>
Import
use thread::{with_write};
Source: compiler/src/builtin_docs/thread.hy

Takes the write lock, runs f and stores the new value it returns.

f returns (new_value, result), as for with_lock. The write lock is held through the callback, so no reader or writer sees a half-done update. Waits while the lock is held.

Parameters#

NameType
lockRwLock
fA -> (A, B)

Returns#

Result<B, ThreadError>

Examples#

use thread::{rwlock, with_write, with_read};
use tests::assert;
let lock = match rwlock(1) {
    Result::Ok(l) => l,
    Result::Err(_) => panic "rwlock failed",
};
let old = match with_write(lock, fn (int n) => (n + 41, n)) {
    Result::Ok(v) => v,
    Result::Err(_) => -1,
};
assert!(old == 1);
assert!(match with_read(lock, fn (int n) => n) { Result::Ok(n) => n == 42, Result::Err(_) => false });

See also#

  • thread::with_lock
  • spawn: Runs f(arg) on another OS thread and returns a handle to it.
  • join: Waits for the thread to finish and returns the value its function returned.
  • detach: Lets the thread run to completion without being joined.
  • channel: Creates an unbounded channel and returns its (Sender, Receiver) ends.
  • send: Sends a copy of value on the channel.
  • recv: Takes the next value from the channel, waiting until one arrives.
  • try_send: Sends value without waiting.
↑ ↓ navigate ↵ open