Function in thread compiler

try_lock

fn try_lock(Mutex m) -> Result<unit, ThreadError>
Import
use thread::{try_lock};
Source: 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#

NameType
mMutex

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::unlock
  • 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