|
2 | 2 |
|
3 | 3 | # Introduction
|
4 | 4 |
|
5 |
| -Rust supports a system of lightweight tasks, similar to what is found |
6 |
| -in Erlang or other actor systems. Rust tasks communicate via messages |
7 |
| -and do not share data. However, it is possible to send data without |
8 |
| -copying it by making use of [the exchange heap](#unique-boxes), which |
9 |
| -allow the sending task to release ownership of a value, so that the |
10 |
| -receiving task can keep on using it. |
11 |
| - |
12 |
| -> ***Note:*** As Rust evolves, we expect the task API to grow and |
13 |
| -> change somewhat. The tutorial documents the API as it exists today. |
| 5 | +Rust supports concurrency and parallelism through lightweight tasks. |
| 6 | +Rust tasks are significantly cheaper to create than traditional |
| 7 | +threads, with a typical 32-bit system able to run hundreds of |
| 8 | +thousands simultaneously. Tasks in Rust are what are often referred to |
| 9 | +as _green threads_, cooperatively scheduled by the Rust runtime onto a |
| 10 | +small number of operating system threads. |
| 11 | + |
| 12 | +Tasks provide failure isolation and recovery. When an exception occurs |
| 13 | +in rust code (either by calling `fail` explicitly or by otherwise performing |
| 14 | +an invalid operation) the entire task is destroyed - there is no way |
| 15 | +to `catch` an exception as in other languages. Instead tasks may monitor |
| 16 | +each other to detect when failure has occurred. |
| 17 | + |
| 18 | +Rust tasks have dynamically sized stacks. When a task is first created |
| 19 | +it starts off with a small amount of stack (in the hundreds to |
| 20 | +low thousands of bytes, depending on plattform), and more stack is |
| 21 | +added as needed. A Rust task will never run off the end of the stack as |
| 22 | +is possible in many other languages, but they do have a stack budget, |
| 23 | +and if a Rust task exceeds its stack budget then it will fail safely. |
| 24 | + |
| 25 | +Tasks make use of Rust's type system to provide strong memory safety |
| 26 | +guarantees, disallowing shared mutable state. Communication between |
| 27 | +tasks is facilitated by the transfer of _owned_ data through the |
| 28 | +global _exchange heap_. |
| 29 | + |
| 30 | +This tutorial will explain the basics of tasks and communication in Rust, |
| 31 | +explore some typical patterns in concurrent Rust code, and finally |
| 32 | +discuss some of the more exotic synchronization types in the standard |
| 33 | +library. |
| 34 | + |
| 35 | +# A note about the libraries |
| 36 | + |
| 37 | +While Rust's type system provides the building blocks needed for safe |
| 38 | +and efficient tasks, all of the task functionality itself is implemented |
| 39 | +in the core and standard libraries, which are still under development |
| 40 | +and do not always present a nice programming interface. |
| 41 | + |
| 42 | +In particular, there are currently two independent modules that provide |
| 43 | +a message passing interface to Rust code: `core::comm` and `core::pipes`. |
| 44 | +`core::comm` is an older, less efficient system that is being phased out |
| 45 | +in favor of `pipes`. At some point the existing `core::comm` API will |
| 46 | +be romoved and the user-facing portions of `core::pipes` will be moved |
| 47 | +to `core::comm`. In this tutorial we will discuss `pipes` and ignore |
| 48 | +the `comm` API. |
| 49 | + |
| 50 | +For your reference, these are the standard modules involved in Rust |
| 51 | +concurrency at the moment. |
| 52 | + |
| 53 | +* [`core::task`] - All code relating to tasks and task scheduling |
| 54 | +* [`core::comm`] - The deprecated message passing API |
| 55 | +* [`core::pipes`] - The new message passing infrastructure and API |
| 56 | +* [`std::comm`] - Higher level messaging types based on `core::pipes` |
| 57 | +* [`std::sync`] - More exotic synchronization tools, including locks |
| 58 | +* [`std::arc`] - The ARC type, for safely sharing immutable data |
| 59 | + |
| 60 | +[`core::task`]: core/task.html |
| 61 | +[`core::comm`]: core/comm.html |
| 62 | +[`core::pipes`]: core/pipes.html |
| 63 | +[`std::comm`]: std/comm.html |
| 64 | +[`std::sync`]: std/sync.html |
| 65 | +[`std::arc`]: std/arc.html |
| 66 | + |
14 | 67 |
|
15 | 68 | # Spawning a task
|
16 | 69 |
|
|
0 commit comments