Practice ยท 1 of 4
The Counter That Loses Nothing
CHALLENGE
Difficulty: intermediate+25 XP
Implement a Counter that stays EXACT under concurrent callers. Increment must call CjConcurrency.Pause() once (simulated work) โ where you put it relative to the update decides everything. Also implement Drain() returning the count and resetting to 0 atomically.
``csharp
public sealed class Counter
{
public void Increment(); // calls CjConcurrency.Pause(); exact under contention
public int Value { get; } // completed increments so far
public int Drain(); // returns count, resets to 0
}
``
Press Submit to check your solution.
Back to lesson: Practice: Concurrency Practice: Counters, Limits, Deadlocks