Function in thread compiler

lock

fn lock(Mutex m) -> Result<unit, ThreadError>
Import
use thread::{lock};
Source: compiler/src/builtin_docs/thread.hy

Locks m until unlock, waiting while another thread holds it.

This gives no access to the value (use with_lock for that); it only excludes other lock / with_lock callers. A thread can hold one mutex through lock at a time: a second lock before unlock is Err(ThreadError::Other).

Parameters#

NameType
mMutex

Returns#

Result<unit, ThreadError>

Examples#

use thread::{mutex, lock, unlock};
use tests::assert;
let m = match mutex(0) {
    Result::Ok(m) => m,
    Result::Err(_) => panic "mutex failed",
};
assert!(match lock(m) { Result::Ok(_) => true, Result::Err(_) => false });
assert!(match unlock(m) { Result::Ok(_) => true, Result::Err(_) => false });

See also#

  • thread::unlock
  • 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.
↑ ↓ navigate ↵ open