Fearless Concurrency
One of Rust’s main goals is "Fearless Concurrency." This means that the type system and the Borrow Checker prevent most common concurrency bugs (like data races) at compile time.
Spawning Threads
You create a new thread in Rust using the
thread::spawn function and passing it a closure containing the code you want to run.rust
Wait! If the main thread finishes, the spawned thread will be shut down immediately, even if it's not done. To wait for a thread to finish, you use a
JoinHandle.Using Join Handles
thread::spawn returns a JoinHandle. Calling .join() on it will block the current thread until the spawned thread has completed its work.rust
Moving Data into Threads
Because a spawned thread might live longer than the main thread, it cannot "borrow" local variables from the main thread. You must use the
move keyword to transfer ownership of the data into the thread.rust
Summary
| Term | Syntax | Description |
|---|---|---|
| spawn | `thread::spawn( | |
| join | handle.join() | Wait for a thread to finish |
| move | `move | |
| sleep | thread::sleep(d) | Pausing the current thread |
| Safety | Compile-time | Prevents data races via ownership rules |
| Handle | JoinHandle<T> | Object returned by spawn |
| Best For | Parallel processing, long-running tasks | |
| Panics | Propagation | If a thread panics, join() returns an Err |
| Memory | Stack size | Default is usually 2MB per thread |
| Efficiency | OS Threads | Rust uses 1:1 threads (one OS thread per Rust thread) |
| Logic | Threads have no built-in way to share data (use Channels!) | |
| Constraint | Data moved into a thread must be 'static or owned | |
| Identity | thread::current().id() (Getting the thread ID) |