Starting threads v0.1.0

Source
Starting a thread on a context, its options, and joining it for its result.

ThreadOptions

pub struct ThreadOptions {
    // private fields
}

How a thread is started: its stack size, for now. Copy.

Source

Constructors

ThreadOptions

pub func ThreadOptions() -> ThreadOptions

The defaults: the system's own stack size.

Returns: the default options

Source

Methods

WithStackSize

pub func WithStackSize(
    self: &ThreadOptions,
    size: uint
) -> ThreadOptions

These options with a stack of size bytes. Zero asks for the system's default. The system rounds the size up to its page size, and refuses one below its minimum.

NameTypeDescription
sizeuintthe stack size in bytes

Returns: the changed options

Source

StackSize

pub func StackSize(self: &ThreadOptions) -> uint

The stack size these options ask for, or zero for the system's default.

Returns: the stack size in bytes

Source

JoinHandle<R>

pub struct JoinHandle<R> {
    // private fields
}

A started thread, and the result it will leave. Joining waits for the thread and hands back what its function returned. Dropping the handle without joining lets the thread run on and clean up after itself; nothing can then wait for it. A handle is not copied.

Source

Type parameters

NameDescription
Rthe type of the thread's result

Methods

Join

pub func Join(self: &var JoinHandle<R>) -> R

Waits for the thread to finish and returns what its function returned. Joining a handle that was already joined or detached is a program error and panics.

Returns: the thread's result

Source

Detach

pub func Detach(self: &var JoinHandle<R>)

Lets the thread run on without anyone waiting for it. It releases itself, and its result, when it finishes. Detaching a handle that was already joined or detached does nothing.

Source

IsJoinable

pub func IsJoinable(self: &JoinHandle<R>) -> bool

Whether the handle can still be joined.

Returns: false once it was joined or detached

Source

Destructor

~JoinHandle

func ~JoinHandle(self: &var JoinHandle<R>)

Source

Spawn<T, R>

pub func Spawn<T: Sendable, R: Sendable>(
    entry: func(T) -> R,
    context: T
) -> JoinHandle<R> ! ThreadError

Starts a thread running entry(context) with the system's default stack size. The context moves to the new thread. Whatever entry returns waits in the handle until it is joined. A panic on any thread ends the whole process, as it does on the main one.

Source

Type parameters

NameDescription
T: Sendablethe type of the context
R: Sendablethe type of the thread's result

Parameters

NameTypeDescription
entryfunc(T) -> Rthe function the thread runs
contextTthe value the function is called with

Returns: a handle to join the thread with

SpawnWith<T, R>

pub func SpawnWith<T: Sendable, R: Sendable>(
    options: ThreadOptions,
    entry: func(T) -> R,
    context: T
) -> JoinHandle<R> ! ThreadError

Starts a thread running entry(context) as options say.

Source

Type parameters

NameDescription
T: Sendablethe type of the context
R: Sendablethe type of the thread's result

Parameters

NameTypeDescription
optionsThreadOptionshow to start the thread
entryfunc(T) -> Rthe function the thread runs
contextTthe value the function is called with

Returns: a handle to join the thread with