try_lock
fn try_lock(Mutex m) -> Result<unit, ThreadError> Import
Source: use thread::{try_lock}; compiler/src/builtin_docs/thread.hy Locks m if it is free, without waiting.
Returns Err(ThreadError::WouldBlock) when the mutex is held (also by
this thread). On success, release it with unlock.
Parameters#
| Name | Type |
|---|---|
m | Mutex |
Returns#
Result<unit, ThreadError>
Examples#
use thread::{mutex, lock, try_lock, unlock, ThreadError};
use tests::assert;
let m = match mutex(0) {
Result::Ok(m) => m,
Result::Err(_) => panic "mutex failed",
};
let _ = lock(m);
let busy = match try_lock(m) {
Result::Err(ThreadError::WouldBlock) => true,
default => false,
};
assert!(busy);
let _ = unlock(m);
assert!(match try_lock(m) { Result::Ok(_) => true, Result::Err(_) => false });
let _ = unlock(m);
See also#
thread::unlockspawn: 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.