Module std::task[src]

Task creation

An executing Rust program consists of a collection of tasks, each with their own stack and local state. A Rust task is typically backed by an operating system thread, making tasks 'just threads', but may also be implemented via other strategies as well (e.g. Rust comes with the green scheduling crate for creating tasks backed by green threads).

Tasks generally have their memory isolated from each other by virtue of Rust's owned types (which of course may only be owned by a single task at a time). Communication between tasks is primarily done through channels, Rust's message-passing types, though other forms of task synchronization are often employed to achieve particular performance goals. In particular, types that are guaranteed to be threadsafe are easily shared between threads using the atomically-reference-counted container, Arc.

Fatal logic errors in Rust cause task failure, during which a task will unwind the stack, running destructors and freeing owned resources. Task failure is unrecoverable from within the failing task (i.e. there is no 'try/catch' in Rust), but failure may optionally be detected from a different task. If the main task fails the application will exit with a non-zero exit code.

Basic task scheduling

By default, every task is created with the same "flavor" as the calling task. This flavor refers to the scheduling mode, with two possibilities currently being 1:1 and M:N modes. Green (M:N) tasks are cooperatively scheduled and native (1:1) tasks are scheduled by the OS kernel.

Example

fn main() { spawn(proc() { println!("Hello, World!"); }) }
spawn(proc() {
    println!("Hello, World!");
})

Advanced task scheduling

Task spawning can also be configured to use a particular scheduler, to redirect the new task's output, or to yield a future representing the task's final result. The configuration is established using the TaskBuilder API:

Example

extern crate green; extern crate native; use std::task::TaskBuilder; use green::{SchedPool, PoolConfig, GreenTaskBuilder}; use native::NativeTaskBuilder; fn main() { // Create a green scheduler pool with the default configuration let mut pool = SchedPool::new(PoolConfig::new()); // Spawn a task in the green pool let mut fut_green = TaskBuilder::new().green(&mut pool).try_future(proc() { /* ... */ }); // Spawn a native task let mut fut_native = TaskBuilder::new().native().try_future(proc() { /* ... */ }); // Wait for both tasks to finish, recording their outcome let res_green = fut_green.unwrap(); let res_native = fut_native.unwrap(); // Shut down the green scheduler pool pool.shutdown(); }
extern crate green;
extern crate native;

use std::task::TaskBuilder;
use green::{SchedPool, PoolConfig, GreenTaskBuilder};
use native::NativeTaskBuilder;

// Create a green scheduler pool with the default configuration
let mut pool = SchedPool::new(PoolConfig::new());

// Spawn a task in the green pool
let mut fut_green = TaskBuilder::new().green(&mut pool).try_future(proc() {
    /* ... */
});

// Spawn a native task
let mut fut_native = TaskBuilder::new().native().try_future(proc() {
    /* ... */
});

// Wait for both tasks to finish, recording their outcome
let res_green  = fut_green.unwrap();
let res_native = fut_native.unwrap();

// Shut down the green scheduler pool
pool.shutdown();

Structs

SiblingSpawner

The default task spawner, which spawns siblings to the current task.

TaskBuilder

The task builder type.

Traits

Spawner

A means of spawning a task

Functions

deschedule

Yield control to the task scheduler.

failing

True if the running task is currently failing (e.g. will return true inside a destructor that is run while unwinding the stack after a call to fail!()).

spawn

Creates and executes a new child task

try

Execute a function in a newly-spawned task and return either the return value of the function or an error if the task failed.

try_future

Execute a function in another task and return a future representing the task's result.

with_task_name

Read the name of the current task.