Skip to main content
Module 10 · Lesson 3 of 423/24 in the course~22 min
Module lessons (3/4)

OS Threads, move Closure and mpsc Channels

Lesson preview5 hands-on exercises readyCourse Rust · Module 10 — Advanced Traits and Fearless Concurrency

Start thread with std::thread::spawn, transfer ownership with move, wait with join(), and communicate with mpsc::channel.

What you'll learn in this lesson

  • Create native OS threads with std::thread::spawn and wait for them to complete using handle.join().unwrap().
  • Securely transfer ownership of captured data into threads with closures move.
  • Exchange messages between threads (Do not communicate by sharing memory) via mpsc::channel() channels.

Overview

Concurrency in many languages generates race conditions that are subtle and difficult to reproduce. Rust applies the same Ownership and Borrowing rules to thread boundaries, turning potential production bugs into immediate compilation errors.

std::thread::spawn accepts a closure and launches a new operating system thread, returning a JoinHandle<T>. Calling .join() blocks the current thread until the worker finishes and gets its return value Result<T, ...>:

Sections covered in this lesson

  1. 11. Start OS thread and handle.join()
  2. 22. Because move is almost always mandatory
  3. 33. Channels std::sync::mpsc (Multi-Producer, Single-Consumer)
  4. 44. Sending with automatic transfer of ownership
  5. 5Practical exercises
  6. 6Check for understanding
Pro Lesson · Module 10 — Advanced Traits and Fearless Concurrency

Unlock "OS Threads, move Closure and mpsc Channels" and the full Course Rust curriculum

Start thread with `std::thread::spawn`, transfer ownership with `move`, wait with `join()`, and communicate with `mpsc::channel`.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Course Rust plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.