Hone logo
Hone
Problems

Concurrent Task Spawning in Rust

Modern applications often benefit from performing multiple operations simultaneously. In Rust, this is achieved through concurrency, and a fundamental building block is the ability to spawn new tasks that can run independently. This challenge focuses on implementing a system for creating and managing these concurrent tasks.

Problem Description

Your goal is to build a mechanism that allows spawning new "tasks" (represented by closures) that execute concurrently. These tasks should be able to be initiated and then run in the background without blocking the main thread. You will need to manage the lifecycle of these spawned tasks to ensure they complete or are handled appropriately.

Key Requirements:

  • Task Spawning: Implement a function that takes a closure as an argument and spawns it to run as a separate task.
  • Concurrency: The spawned tasks must execute concurrently with the main program flow and with each other.
  • Task Completion (Optional but Recommended): While not strictly required for basic spawning, consider how you might be able to wait for tasks to finish or retrieve their results.
  • Error Handling: Consider how to handle potential errors during task creation or execution.

Expected Behavior:

When a task is spawned, it should begin execution shortly after being initiated. The main thread should be able to continue its work without waiting for the spawned task to complete, unless explicitly designed to do so.

Important Edge Cases:

  • Spawning a large number of tasks.
  • Tasks that take a significant amount of time to complete.
  • Tasks that panic or return errors.

Examples

Example 1:

Input:
A function that spawns two tasks.
Task 1: Prints "Task 1 started" and then "Task 1 finished" after a short delay.
Task 2: Prints "Task 2 started" and then "Task 2 finished" after a slightly different short delay.
The main thread prints "Main thread continuing".

Expected Output (order of prints may vary due to concurrency):
Main thread continuing
Task 1 started
Task 2 started
Task 1 finished
Task 2 finished

Explanation: The main thread initiates the spawning of two tasks. While the tasks are running their respective operations (including delays), the main thread continues executing and prints its message. The output order of the task-related messages can vary because they are running concurrently.

Example 2:

Input:
A function that spawns a task that immediately panics.
The main thread attempts to spawn this task.

Expected Output:
The program should terminate, and the panic message from the spawned task should be visible in the stderr.

Explanation: This example demonstrates error handling in a spawned task. A panic in a spawned task typically causes the entire program to terminate unless specific mechanisms are in place to catch or handle such panics.

Constraints

  • The solution must be written in Rust.
  • You should use Rust's standard library or widely accepted crates for concurrency (e.g., std::thread).
  • The primary focus is on demonstrating the spawning mechanism. Advanced synchronization primitives are optional for the core challenge but encouraged for a more robust solution.

Notes

  • Consider the difference between std::thread::spawn and other concurrency models you might be aware of.
  • Think about how you would signal completion or pass data between tasks if you were to extend this.
  • The use of std::time::Duration for introducing delays can be helpful in observing concurrent behavior.
Loading editor...
rust