with_write
fn with_write<A, B>(RwLock lock, A -> (A, B) f) -> Result<B, ThreadError> Import
Source: use thread::{with_write}; 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#
| Name | Type |
|---|---|
lock | RwLock |
f | A -> (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_lockspawn: Runsf(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 ofvalueon the channel.recv: Takes the next value from the channel, waiting until one arrives.try_send: Sendsvaluewithout waiting.