with_read
fn with_read<A, B>(RwLock lock, A -> B f) -> Result<B, ThreadError> Import
Source: use thread::{with_read}; compiler/src/builtin_docs/thread.hy Runs f on a snapshot of the lock’s value and returns what f returns.
The read lock is released before f runs, so another thread may change
the value meanwhile. Waits while a writer holds the lock (blocking every
task of the thread, also inside a task::scope).
Parameters#
| Name | Type |
|---|---|
lock | RwLock |
f | A -> B |
Returns#
Result<B, ThreadError>
Examples#
use thread::{rwlock, with_read};
use tests::assert;
let lock = match rwlock([1, 2, 3]) {
Result::Ok(l) => l,
Result::Err(_) => panic "rwlock failed",
};
let first = match with_read(lock, fn ([int; 3] xs) => xs[0]) {
Result::Ok(v) => v,
Result::Err(_) => -1,
};
assert!(first == 1);
See also#
task::scopespawn: 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.