Hone logo
Hone
Problems

Concurrent Data Processing with Crossbeam Channels

This challenge focuses on utilizing the crossbeam crate in Rust to implement a concurrent data processing pipeline. You'll be building a system that receives a stream of integers, squares each integer, and then outputs the squared values to another channel. This demonstrates a common pattern for parallelizing tasks and efficiently communicating between threads.

Problem Description

You are tasked with creating a Rust program that uses crossbeam channels to process a stream of integers concurrently. The program should consist of three main components:

  1. Producer: Generates a sequence of integers and sends them to a Sender of a crossbeam::channel::unbounded::Sender.
  2. Worker: Receives integers from the first channel, squares each integer, and sends the squared value to a Sender of a crossbeam::channel::unbounded::Sender.
  3. Consumer: Receives squared integers from the second channel and prints them to the console.

The producer and worker should run in separate threads. The consumer should also run in a separate thread. The program should terminate gracefully after the producer has finished sending all integers.

Key Requirements:

  • Utilize crossbeam::channel::unbounded::Sender and crossbeam::channel::unbounded::Receiver for inter-thread communication.
  • Implement the producer, worker, and consumer as separate functions.
  • Use std::thread to spawn the worker and consumer threads.
  • Handle potential errors gracefully (though error handling beyond channel closure is not required for this challenge).
  • Ensure the program terminates correctly after all integers have been processed.

Expected Behavior:

The program should receive a sequence of integers, square each integer concurrently using a worker thread, and print the squared values to the console in the order they were processed. The program should terminate cleanly after all integers have been processed.

Edge Cases to Consider:

  • Empty input sequence: The program should terminate gracefully without errors.
  • Large input sequence: The program should handle a large number of integers efficiently using concurrency.

Examples

Example 1:

Input: [1, 2, 3, 4, 5]
Output: [1, 4, 9, 16, 25]
Explanation: The producer sends the integers 1 through 5. The worker squares each integer concurrently and sends the results to the consumer. The consumer prints the squared values to the console.

Example 2:

Input: []
Output: (No output - program terminates)
Explanation: The producer sends an empty sequence. The worker receives nothing and the consumer receives nothing. The program terminates gracefully.

Example 3:

Input: [1000000, 2000000, 3000000]
Output: [1000000000, 4000000000, 9000000000]
Explanation: Demonstrates handling larger numbers and the benefit of concurrent processing.

Constraints

  • The input sequence of integers will be provided as a Vec<i32>.
  • The program must use crossbeam channels for inter-thread communication.
  • The worker thread must square the integers.
  • The program should terminate gracefully after processing all integers.
  • Performance is a consideration; aim for efficient concurrent processing. While benchmarking isn't required, avoid unnecessary overhead.

Notes

  • Consider using drop() on the Sender in the producer to signal the end of the input stream to the worker. This is a standard way to indicate completion in crossbeam channels.
  • The Receiver will automatically close when the corresponding Sender is dropped, allowing for clean termination.
  • Think about how to structure your code to clearly separate the producer, worker, and consumer responsibilities.
  • The crossbeam crate provides excellent documentation and examples; refer to it for more details on channel usage.
Loading editor...
rust