Starting threads v0.1.0
ThreadOptions
pub struct ThreadOptions {
// private fields
}
How a thread is started: its stack size, for now. Copy.
Constructors
ThreadOptions
pub func ThreadOptions() -> ThreadOptions
The defaults: the system's own stack size.
Returns: the default options
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.
| Name | Type | Description |
|---|---|---|
size | uint | the stack size in bytes |
Returns: the changed options
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
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.
Type parameters
| Name | Description |
|---|---|
R | the 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
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.
IsJoinable
pub func IsJoinable(self: &JoinHandle<R>) -> bool
Whether the handle can still be joined.
Returns: false once it was joined or detached
Destructor
~JoinHandle
func ~JoinHandle(self: &var JoinHandle<R>)
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.
Type parameters
| Name | Description |
|---|---|
T: Sendable | the type of the context |
R: Sendable | the type of the thread's result |
Parameters
| Name | Type | Description |
|---|---|---|
entry | func(T) -> R | the function the thread runs |
context | T | the 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.
Type parameters
| Name | Description |
|---|---|
T: Sendable | the type of the context |
R: Sendable | the type of the thread's result |
Parameters
| Name | Type | Description |
|---|---|---|
options | ThreadOptions | how to start the thread |
entry | func(T) -> R | the function the thread runs |
context | T | the value the function is called with |
Returns: a handle to join the thread with