Design a thread-safe integer counter whose read-modify-write operations are atomic.
Implement the AtomicCounter class:
AtomicCounter(initialValue) initializes the counter to initialValue.incrementAndGet() atomically adds 1 and returns the new value.addAndGet(delta) atomically adds delta and returns the new value.get() atomically reads and returns the current value.Every method must be linearizable. Each call must appear to take effect at one instant between its invocation and return. Consequently, concurrent updates cannot be lost, and two increments starting from the same state cannot both return the same new value.
The same counter may be accessed by many threads. The judge creates and coordinates those threads; your implementation should contain only the state and synchronization inside AtomicCounter.
Standard concurrency and thread APIs are preloaded, so you do not need import, include, or using statements.
Input:
Output:
Input:
Output:
Explanation: All 10000 increments are preserved even though the calls overlap.
-1000000000 <= initialValue <= 1000000000-1000000 <= delta <= 100000050000 method calls are made per counter.An expression such as value++ looks like one operation, but it normally performs three steps:
Two threads can read the same old value before either stores its result. Both then write the same new value, so one update disappears. Protecting only the read or only the write does not fix the combined read-modify-write operation.
The returned value matters too. If a counter starts at 0, concurrent increments must collectively return every number from 1 through the number of increments exactly once. Returning the value from a separate later read would allow another update to slip in between the modification and the return.
Use the language's atomic integer primitive when one is available:
delta.Python's standard library does not provide a general-purpose atomic integer, so protect the value with one mutex. The lock must cover both the update and the returned result.
A lock-based implementation is also correct in the other languages, although their native atomic primitives express this abstraction directly.
Each update uses one atomic read-modify-write operation. The runtime assigns those operations a total modification order for the counter. An update adds its delta to the value produced by the preceding operation in that order and returns its own resulting value. Therefore, no two operations overwrite the same old state and no update is lost.
get performs an atomic load, so it observes a complete counter value rather than a torn or partially written value. If an update finishes before get begins, the atomic synchronization guarantees that the load observes that update or a later one.
For the Python implementation, the mutex provides the same argument: only one method can access the value at a time, and each method's critical section is its linearization interval.
O(1) time and space.incrementAndGet: O(1) time and space.addAndGet: O(1) time and space.get: O(1) time and space.These bounds exclude time spent waiting because of contention.