Module compiler

thread

spawn runs a function on a new OS thread with its own VM and heap. Values cross between threads by copy through send/recv on a channel or through a Mutex / RwLock; heap handles such as gc::Root cannot be sent.

For concurrent IO on one thread, tasks are lighter. Threads pay off for CPU-bound work. See the threads tutorial.

Built into the compiler.

Import what you use (use path::* is not allowed):

use thread::{Thread, Sender};

Types#

NameDescription
ThreadA handle to a function running on another OS thread, from spawn.
SenderThe sending half of a channel made by channel.
ReceiverThe receiving half of a channel made by channel.
MutexA mutual-exclusion lock that owns a value, made by mutex.
RwLockA readers-writer lock that owns a value, made by rwlock.
ThreadErrorThe error of every thread function.

Functions#

NameDescription
spawnRuns f(arg) on another OS thread and returns a handle to it.
joinWaits for the thread to finish and returns the value its function returned.
detachLets the thread run to completion without being joined.
channelCreates an unbounded channel and returns its (Sender, Receiver) ends.
sendSends a copy of value on the channel.
recvTakes the next value from the channel, waiting until one arrives.
try_sendSends value without waiting.
try_recvTakes the next value if there is one, without waiting.
closeCloses the channel: no more sends.
mutexCreates a mutex holding a copy of initial.
with_lockLocks m, runs f on its value, stores the new value and returns the result.
lockLocks m until unlock, waiting while another thread holds it.
try_lockLocks m if it is free, without waiting.
unlockReleases a mutex this thread locked with lock or try_lock.
rwlockCreates a readers-writer lock holding a copy of initial.
with_readRuns f on a snapshot of the lock’s value and returns what f returns.
with_writeTakes the write lock, runs f and stores the new value it returns.
try_readLike with_read, but returns Err(ThreadError::WouldBlock) instead of waiting when a writer holds the lock.
try_writeLike with_write, but returns Err(ThreadError::WouldBlock) instead of waiting when the lock is held.
↑ ↓ navigate ↵ open