Hone logo
Hone
Problems

Implementing a Thread-Safe Counter in Rust

Concurrency is a fundamental aspect of modern software development. In Rust, ensuring thread safety is crucial to prevent data races and undefined behavior. This challenge asks you to implement a counter that can be safely incremented by multiple threads concurrently.

Problem Description

Your task is to create a ThreadSafeCounter struct in Rust that allows multiple threads to increment its internal value without causing data races. The counter should start at zero and be incremented a specified number of times by a given number of threads.

Key Requirements:

  1. Thread Safety: The counter must be thread-safe. This means that concurrent access and modification from multiple threads should not lead to unexpected results or memory corruption.
  2. Initialization: The counter should be initialized to 0.
  3. Increment Operation: Provide a method (e.g., increment) that safely increments the counter's value by 1.
  4. Value Retrieval: Provide a method (e.g., get_value) to retrieve the current value of the counter.
  5. Shared Ownership: The counter must be shareable across multiple threads.

Expected Behavior:

If N threads each increment the counter M times, the final value of the counter should be N * M.

Edge Cases:

  • Zero Threads: The program should handle the case where no threads are spawned.
  • Zero Increments: The program should handle the case where threads are spawned but perform zero increments.
  • Large Numbers: Consider the potential for large numbers of threads and increments, though explicit overflow handling for the counter value itself is not the primary focus of this challenge unless it causes data races.

Examples

Example 1:

Number of Threads: 2
Increments per Thread: 100000

Final Counter Value: 200000

Explanation: Two threads are spawned, and each thread increments the counter 100,000 times. The total number of increments is 2 * 100,000 = 200,000. The ThreadSafeCounter ensures that all these increments are correctly registered.

Example 2:

Number of Threads: 5
Increments per Thread: 50000

Final Counter Value: 250000

Explanation: Five threads are spawned, and each thread increments the counter 50,000 times. The total number of increments is 5 * 50,000 = 250,000.

Example 3 (Edge Case):

Number of Threads: 0
Increments per Thread: 100000

Final Counter Value: 0

Explanation: No threads are spawned, so no increments occur, and the counter remains at its initial value of 0.

Constraints

  • The number of threads (N) can range from 0 to 1000.
  • The number of increments per thread (M) can range from 0 to 1,000,000.
  • The solution should utilize Rust's concurrency primitives to achieve thread safety.
  • The final value of the counter should fit within a standard integer type (e.g., u64).

Notes

  • Consider how to safely share ownership of the ThreadSafeCounter across threads. The Arc smart pointer is a strong candidate for this.
  • Think about how to synchronize access to the counter's internal state to prevent multiple threads from reading and writing the value simultaneously in a way that corrupts it. The Mutex or Atomic types are likely to be useful.
  • You will need to spawn threads and wait for them to complete before retrieving the final counter value.
  • The goal is to demonstrate understanding of Rust's thread safety mechanisms, not necessarily to achieve the absolute fastest possible performance (though efficiency is always a good consideration).
Loading editor...
rust