Function in thread compiler

spawn

fn spawn<A, B>(A -> B f, A arg) -> Result<Thread, ThreadError>
Import
use thread::{spawn};
Source: compiler/src/builtin_docs/thread.hy

Runs f(arg) on another OS thread and returns a handle to it.

The work goes to a pool of worker threads (one per CPU by default, set with the COIL_MAX_WORKER_THREADS environment variable). Each thread has its own heap: arg and the use captures of a closure are deep-copied to it, and the result is copied back by join. Sendable values are numbers, strings, tuples, arrays, records, enums and class instances made of them, plus Sender, Receiver, Mutex and RwLock handles, which stay shared. Anything else is Err(ThreadError::NotSendable).

spawn(f) with no argument runs a function that takes none. When main returns, the program still waits for threads that were neither joined nor detached.

Parameters#

NameType
fA -> B
argA

Returns#

Result<Thread, ThreadError>

Examples#

use thread::{spawn, join};
use tests::assert;
fn square(int n) -> int {
    return n * n;
}

fn main() {
    let t = match spawn(square, 7) {
        Result::Ok(t) => t,
        Result::Err(_) => panic "spawn failed",
    };
    let n = match join(t) {
        Result::Ok(n) => n,
        Result::Err(_) => 0,
    };
    assert!(n == 49);
}

See also#

↑ ↓ navigate ↵ open