Function in thread compiler

mutex

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

Creates a mutex holding a copy of initial.

The value must be sendable (see spawn), or the result is Err(ThreadError::NotSendable). Read and change it with with_lock.

Parameters#

NameType
initialT

Returns#

Result<Mutex, ThreadError>

Examples#

use thread::{mutex, with_lock, spawn, join, Mutex};
use tests::assert;
fn bump(Mutex m) -> int {
    let _ = with_lock(m, fn (int n) => (n + 1, 0));
    return 0;
}

fn main() {
    let m = match mutex(0) {
        Result::Ok(m) => m,
        Result::Err(_) => panic "mutex failed",
    };
    let t1 = match spawn(bump, m) { Result::Ok(t) => t, Result::Err(_) => panic "spawn" };
    let t2 = match spawn(bump, m) { Result::Ok(t) => t, Result::Err(_) => panic "spawn" };
    let _ = join(t1);
    let _ = join(t2);
    let n = match with_lock(m, fn (int x) => (x, x)) {
        Result::Ok(n) => n,
        Result::Err(_) => -1,
    };
    assert!(n == 2);
}

See also#

  • thread::spawn
  • thread::with_lock
  • 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