Goroutine Counter
Goroutine Counter
Section titled “Goroutine Counter”Build a thread-safe counter that can be safely incremented from multiple goroutines.
Background
Section titled “Background”When multiple goroutines access shared state concurrently, you need synchronization to prevent race conditions. In this exercise, you’ll build a counter that uses a channel to serialize access.
Your Task
Section titled “Your Task”Implement a Counter struct with:
- A method to increment the counter
- A method to get the current value
- Proper synchronization using channels
The counter should work correctly even when incremented from 100 concurrent goroutines.
Thread-Safe Counter
~10 mineasy
Implement a counter that can be safely used from multiple goroutines
Key Concepts
Section titled “Key Concepts”- Channel-based synchronization: Using channels to serialize access to shared state
- Select statement: Handling multiple channel operations
- Ownership pattern: One goroutine “owns” the state and communicates via channels