AlgoMaster Logo

Design an Atomic Counter

easy8 min readUpdated August 31, 2026
Listen to this chapter
Unlock Audio

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.

Example 1:

Input:

Output:

Example 2:

Input:

Output:

Explanation: All 10000 increments are preserved even though the calls overlap.

Constraints

  • -1000000000 <= initialValue <= 1000000000
  • -1000000 <= delta <= 1000000
  • At most 50000 method calls are made per counter.
  • The counter's value always remains within the signed 32-bit integer range.
  • Any number of method calls may execute concurrently on the same instance.
  • Judge threads are not interrupted while executing a method.

Starter Code

Understanding the Problem

An expression such as value++ looks like one operation, but it normally performs three steps:

  1. Read the current value.
  2. Calculate the next value.
  3. Store the result.

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.

Synchronization Strategy

Use the language's atomic integer primitive when one is available:

  • Atomic increment performs the update and returns the new value as one indivisible operation.
  • Atomic add does the same for an arbitrary delta.
  • Atomic load reads the current value with the primitive's visibility guarantees.

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.

Correctness

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.

Code

Complexity Analysis

  • Constructor: 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.