Using thread concurrency

Rust threads have the following features:

  • Share memory
  • Share resources, such as files or sockets
  • Tend to be thread-safe
  • Support inter-thread messaging
  • Are platform-independent

For the preceding reasons, we suggest that Rust threads are better suited to most concurrency use cases than subprocesses. If you want to distribute computation, circumvent a blocking operation, or otherwise utilize concurrency for your application—use threads.

To show the thread pattern, we can re-implement the preceding examples. Here are three children threads:

use std::{thread,time};use std::process;extern crate thread_id;fn main() {   for _ in 0..3 {      thread::spawn(|| {         let t = time::Duration::from_millis(1000);         loop { println!("child thread ...

Get Hands-On Functional Programming in Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.