Hone logo
Hone
Problems

Mastering Thread Spawning in Rust

This challenge focuses on the fundamental concept of concurrency in Rust: spawning new threads. Understanding how to create and manage threads is crucial for building responsive and efficient applications that can perform multiple tasks simultaneously. You will learn to launch independent execution paths and ensure they complete their work.

Problem Description

Your task is to create a Rust program that spawns multiple threads. Each thread should perform a simple operation, and your main thread should wait for all spawned threads to finish before exiting.

Requirements:

  1. Spawn Multiple Threads: You need to use the std::thread::spawn function to create at least three new threads.
  2. Thread Task: Each spawned thread will print a message indicating its thread ID (or a simple identifier) and the message it is processing.
  3. Main Thread Synchronization: The main thread must wait for all spawned threads to complete their execution. Use thread::JoinHandle::join() for this purpose.
  4. Data Sharing (Simple): Pass a simple piece of data (e.g., a string or an integer) to each thread to demonstrate how to provide work for them.

Expected Behavior:

The program should output messages from each thread. The order of these messages might vary due to the nature of threading, but all messages from all threads should appear before the program terminates. The main thread should not exit until all spawned threads have finished their printing.

Edge Cases to Consider:

  • Thread Panics: While not strictly required to handle in this basic challenge, consider what might happen if a spawned thread panics. (For this challenge, we will assume threads do not panic).
  • Resource Contention (Minor): For this simple problem, resource contention is unlikely to be a major issue, but be aware that in more complex scenarios, multiple threads accessing shared mutable data can lead to race conditions.

Examples

Example 1:

Input: No direct input is provided for this challenge; the program's behavior is inherent.
Output:
Thread 0 processing: "Hello from thread 0!"
Thread 1 processing: "Greetings from thread 1!"
Thread 2 processing: "This is thread 2 reporting!"
Main thread finished waiting for all threads.

(Note: The order of the "Thread X processing" lines may vary.)

Explanation: The program spawns three threads. Each thread is given a unique identifier and a message. They print their respective messages. The main thread then waits for each of them to finish before printing its final message.

Example 2:

Input: No direct input.
Output:
Thread 1 processing: "Work item A"
Thread 0 processing: "Work item B"
Thread 2 processing: "Work item C"
Main thread finished waiting for all threads.

(Note: The order of the "Thread X processing" lines may vary. The messages themselves might differ from Example 1.)

Explanation: This shows another possible execution. The threads are still identifiable, but the specific messages and their order are different, illustrating the non-deterministic nature of thread scheduling.

Constraints

  • You must use std::thread::spawn for creating new threads.
  • You must use std::thread::JoinHandle::join() to wait for threads to complete.
  • Spawn at least three threads.
  • The program should successfully compile and run on a standard Rust installation.

Notes

  • Think about how to pass data into your threads. Closures are commonly used with std::thread::spawn.
  • The move keyword is often essential when working with closures that capture variables from their environment to ensure ownership is transferred to the new thread.
  • Remember to collect the JoinHandles returned by spawn so you can call join on them later.
Loading editor...
rust