Back to series
· 12 min read

Locks and Atomic Operations


I. Why Do We Need Locks and Atomic Operations?

In a multithreaded program, multiple threads may access the same shared data at the same time. Without an appropriate synchronization mechanism, data races, lost updates, and inconsistent states can occur.

For example, two threads increment a counter at the same time:

class Counter {
    int count = 0;

    void increment() {
        count++;
    }
}

It looks like count++ is only one line of code, but it usually contains three logical steps:

  1. Read the current value.
  2. Compute the new value.
  3. Write the new value back.

Assume the initial value is 0, and two threads execute at the same time:

Step Thread A Thread B count
1 Read 0 0
2 Read 0 0
3 Compute 1 0
4 Compute 1 0
5 Write 1 1
6 Write 1 1

The final result is 1, not the expected 2.

This problem is called a lost update.

Both locks and atomic operations can solve this kind of concurrent update problem, but they use different implementation approaches:

  • Lock: Controls the permission for threads to enter a critical section, so that shared operations are executed according to the specified synchronization rules.
  • Atomic Operation: Ensures that a specific operation has indivisible execution semantics, preventing other threads from observing incomplete updates.

II. What Is a Lock?

A lock is a concurrency synchronization mechanism used to coordinate access to shared resources by multiple threads.

After a thread acquires a lock, other threads that need the same lock must wait until the lock is released.

Locks are usually used to protect critical sections.

A critical section is a region of code that accesses or modifies shared resources and must obey specific synchronization constraints.

For example:

class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }

    public synchronized int getCount() {
        return count;
    }
}

Here, synchronized is used to protect the counter.

Two synchronized instance methods on the same object instance use the same object monitor lock, so only one thread can execute a synchronized region within them at a time.

1. Basic Working Principle of Locks

Basic execution process of a lock:

flowchart TD
    A["Thread prepares to access shared resource"] --> B["Try to acquire lock"]
    B --> C{"Acquired successfully?"}
    C -->|"No"| D["Wait for lock release"]
    D --> B
    C -->|"Yes"| E["Enter critical section"]
    E --> F["Read or modify shared data"]
    F --> G["Exit critical section"]
    G --> H["Release lock"]
    H --> I["Other threads can compete to acquire the lock"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style C fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style D fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style H fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

The core role of a lock is not only to prevent multiple threads from executing code at the same time; it can also establish the necessary memory synchronization relationships through the language memory model.

For example, in Java, an unlock operation on a monitor Happens-Before a subsequent lock operation on the same monitor by another thread. This allows modifications made by the previous thread before releasing the lock to be observed by the subsequent thread in accordance with the memory model.

2. What Is a Critical Section?

A critical section is a region of shared-state access that needs protection.

For example, a bank account:

class Account {
    private int balance = 1000;

    public synchronized boolean withdraw(int amount) {
        if (balance >= amount) {
            balance -= amount;
            return true;
        }
        return false;
    }
}

What needs to be protected here is not the subtraction alone, but the entire business operation:

  • Check whether the balance is sufficient.
  • Deduct the account balance.
  • Return the operation result.

If only balance -= amount is locked without protecting the preceding balance check, concurrent withdrawal problems can still occur.

The boundaries of a critical section should be determined by business invariants, not simply by the number of lines of code.

3. Basic Properties of Locks

Property Meaning
Mutual exclusion A mutex lock generally allows only one thread to hold it at a time
Visibility Correct lock release and acquisition can establish a memory synchronization relationship
Ordering Synchronization rules constrain reordering of related operations
Blocking or waiting A thread that cannot acquire the lock may wait
Resource management Locks need to be correctly acquired, released, and lifecycle-managed

Note that different lock implementations and semantics are not exactly the same. For example, a spinlock may not immediately block the thread; instead, it keeps the thread trying to acquire the lock.

III. Common Types of Locks

Different scenarios require different locking mechanisms. When choosing a lock, consider the ratio of concurrent reads to writes, the size of the critical section, the degree of contention, and fairness requirements.

1. Mutex

A mutex (Mutual Exclusion Lock) is the most common synchronization mechanism.

It guarantees that only one thread can hold the lock at any given time.

For example, in C++:

#include <iostream>
#include <mutex>
#include <thread>

int counter = 0;
std::mutex mtx;

void increment() {
    std::lock_guard<std::mutex> lock(mtx);
    counter++;
}

int main() {
    std::thread t1(increment);
    std::thread t2(increment);

    t1.join();
    t2.join();

    std::cout << counter << std::endl;
}

Here:

  • std::mutex creates a mutex.
  • lock_guard acquires the lock upon construction.
  • The lock is automatically released when leaving the scope.
  • counter++ executes under the protection of the lock.

Mutexes are suitable for:

  • Updating shared variables.
  • Modifying account balances.
  • Operating on shared containers.
  • Maintaining multi-field state.
  • Scenarios that require protecting an entire business operation.

2. Reentrant Lock

A reentrant lock allows a thread that already holds the lock to acquire the same lock again.

For example, in Java:

import java.util.concurrent.locks.ReentrantLock;

class Service {
    private final ReentrantLock lock = new ReentrantLock();

    public void methodA() {
        lock.lock();
        try {
            methodB();
        } finally {
            lock.unlock();
        }
    }

    public void methodB() {
        lock.lock();
        try {
            System.out.println("执行方法 B");
        } finally {
            lock.unlock();
        }
    }
}

When a thread executes methodA(), it has already acquired the lock; after calling methodB(), the same thread can acquire the lock again.

A reentrant lock typically records the owning thread and the reentry count.

Each successful lock acquisition increments the hold count, and only after the corresponding number of releases has been completed does the lock actually become available.

Suitable for:

  • Recursive calls.
  • Multi-layer method calls.
  • Scenarios where lock nesting exists between methods within the same object.

However, reentrancy does not mean lock design can be ignored. Deadlocks can still occur, and it cannot resolve circular waiting for resources between different threads.

3. Read-Write Lock

A read-write lock divides access operations into two categories: reads and writes.

It usually has the following rules:

  • Multiple threads can hold the read lock simultaneously.
  • The write lock requires exclusive access.
  • The write lock generally cannot be held at the same time as other read locks or write locks.

For example, in Java:

import java.util.concurrent.locks.ReentrantReadWriteLock;

class Cache {
    private final ReentrantReadWriteLock lock =
        new ReentrantReadWriteLock();

    private String value = "initial";

    public String read() {
        lock.readLock().lock();
        try {
            return value;
        } finally {
            lock.readLock().unlock();
        }
    }

    public void write(String newValue) {
        lock.writeLock().lock();
        try {
            value = newValue;
        } finally {
            lock.writeLock().unlock();
        }
    }
}

A read-write lock is suited to scenarios with many reads and few writes, for example:

  • Configuration center caches.
  • Metadata reads.
  • Read-only indexes.
  • Shared dictionaries.

But a read-write lock is not necessarily faster than an ordinary mutex lock.

When there are many write operations, the critical section is very short, or lock contention is not obvious, the management overhead of a read-write lock may offset the gains from concurrent reads.

4. Spinlock

A spinlock is a locking mechanism in which a thread keeps looping and retrying after failing to acquire the lock.

Typical logic:

while (lock_is_busy()) {
    // keep retrying
}

acquire_lock();

Unlike blocking locks, a spinlock usually does not put the thread to sleep to wait immediately; instead, it keeps checking the lock state.

Advantages:

  • Avoids the overhead of blocking and waking up threads in some cases.
  • Suited to scenarios with very short critical sections.
  • May have lower acquisition latency in multi-core environments.

Disadvantages:

  • Continuously consumes CPU while waiting.
  • Performance may be poor when the critical section is too long.
  • May cause severe contention on a single core or when CPU resources are tight.

Spinlocks are better suited to scenarios where locks are held for a short time and contention is controllable.

5. Fair Locks and Non-Fair Locks

Fairness describes the scheduling policy by which multiple waiting threads acquire a lock.

Fair lock:

Usually allocates the lock in the order of waiting, reducing the risk of some threads being unable to obtain the lock for a long time.

Non-fair lock:

Allows newly arriving threads to join the competition, which may improve throughput, but waiting times are not necessarily fair.

For example, in Java:

ReentrantLock fairLock = new ReentrantLock(true);
ReentrantLock unfairLock = new ReentrantLock(false);

A fair lock does not mean absolute fairness, nor does it mean starvation definitely will not occur. The actual behavior is still affected by the scheduler, the lock implementation, and system load.

6. Comparison of Lock Types

Lock Type Core Characteristic Typical Scenario
Mutex lock Held by one thread at a time General shared-state protection
Reentrant lock The same thread can acquire it repeatedly Nested calls, recursion
Read-write lock Allows concurrent reads, exclusive writes Many reads, few writes
Spinlock Continuously retries to acquire the lock Extremely short critical sections
Fair lock Allocates as much as possible in waiting order When waiting fairness matters
Non-fair lock Allows competition and queue jumping When throughput matters

IV. What Is an Atomic Operation?

An atomic operation is an operation that is indivisible in concurrent semantics.

An atomic operation either completes entirely or produces no observable result of partial completion.

For example:

AtomicInteger counter = new AtomicInteger(0);

counter.incrementAndGet();

Here the increment operation is provided by an atomic class, so it will not suffer lost updates caused by ordinary read-modify-write when multiple threads execute it simultaneously.

The key to an atomic operation is not that the code is only one line, but that the operation itself is guaranteed atomicity by the language memory model and the underlying implementation.

1. The Difference Between Atomic Operations and Ordinary Operations

Ordinary increment:

int count = 0;
count++;

Atomic increment:

AtomicInteger count = new AtomicInteger(0);
count.incrementAndGet();

The difference between the two:

Comparison dimension Plain increment Atomic increment
Operation semantics Read, compute, write Indivisible atomic update
Concurrency safety Requires additional synchronization Certain atomic update operations are concurrency-safe
Lost updates May occur Do not occur because of that atomic operation
Implementation Ordinary memory access Atomic instructions, CAS, or other mechanisms
Applicable scope Single-threaded, or already protected by synchronization Suited to simple shared-state updates

2. Basic Characteristics of Atomic Operations

Indivisibility

Other threads cannot observe the intermediate state of an atomic update operation.

Linearizability semantics

Many atomic operations can be understood as completing at a single instant, as if the operations of multiple threads occurred one after another in some legal order.

Concurrency safety

Provided atomic operations are used correctly, data races in updates to specific shared variables can be avoided.

Not necessarily lock-free

Atomic operations do not mean the underlying implementation is necessarily free of locks. On some platforms or for some types, atomic semantics may be implemented through internal locks.

V. CAS: One of the Core Mechanisms of Atomic Operations

CAS (Compare-And-Swap) is a common atomic update mechanism.

It usually involves three values:

  • V: the current value in memory.
  • A: the expected old value.
  • B: the new value to be written.

Execution rules of CAS:

  1. Read the current value V.
  2. Compare whether V equals the expected value A.
  3. If they are equal, update V to B.
  4. If they are not equal, do not update.
  5. Return whether the operation succeeded.

1. The Execution Process of CAS

flowchart TD
    A["Read current value V"] --> B["Prepare expected value A and new value B"]
    B --> C["Execute CAS"]
    C --> D{"Does V equal A?"}
    D -->|"Yes"| E["Atomically write B"]
    E --> F["Operation succeeds"]
    D -->|"No"| G["Update fails"]
    G --> H["Re-read the current value"]
    H --> B

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style G fill:#FBE9E9,stroke:#D9AAAA,color:#334155
    style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px

The key to CAS is that the comparison and the update must be completed as a single atomic operation.

If two threads simultaneously try to update the same value from 0 to 1:

  • Thread A's CAS succeeds.
  • Thread B's expected value no longer matches.
  • Thread B's CAS fails.
  • Thread B can re-read and compute a new value.

This prevents two threads from unconditionally overwriting each other's updates.

2. CAS in Java

Java provides a variety of atomic classes:

import java.util.concurrent.atomic.AtomicInteger;

class Counter {
    private final AtomicInteger count = new AtomicInteger(0);

    public void increment() {
        count.incrementAndGet();
    }

    public int get() {
        return count.get();
    }
}

Common atomic classes include:

Type Purpose
AtomicInteger Atomic integer
AtomicLong Atomic long integer
AtomicBoolean Atomic boolean value
AtomicReference Atomic object reference
AtomicIntegerArray Atomic integer array
LongAdder Counter for high-concurrency accumulation scenarios

AtomicInteger is commonly used for simple counters, while LongAdder is suited to statistical accumulation under high contention. The two differ in read semantics and performance characteristics, and are not fully interchangeable.

3. The ABA Problem of CAS

CAS has a classic problem: ABA (the ABA Problem).

Suppose a variable starts out with the value A:

  1. Thread A reads A.
  2. Thread B changes A to B.
  3. Thread B changes B back to A.
  4. Thread A performs CAS and finds that the current value is still A.
  5. The CAS succeeds.

Thread A cannot tell from the current value alone whether the variable has ever changed.

If the business logic requires detecting intermediate state changes, this can become a problem.

One way to solve it is to use a version number or a timestamp.

For example, in Java:

import java.util.concurrent.atomic.AtomicStampedReference;

class Example {
    private final AtomicStampedReference<String> reference =
        new AtomicStampedReference<>("A", 0);

    public void update() {
        int[] stamp = new int[1];

        String value = reference.get(stamp);

        reference.compareAndSet(
            value,
            "B",
            stamp[0],
            stamp[0] + 1
        );
    }
}

Here, in addition to comparing the object reference, the version stamp is also compared.

Even if the object reference goes from A to B and back to A, the version stamp helps identify the state change.

6. The Difference Between Locks and Atomic Operations

Both locks and atomic operations can be used for concurrency synchronization, but they apply to different situations.

Aspect Lock Atomic operation
Core mechanism Controls access to a critical section Guarantees that a specific operation is indivisible
Scope of protection Can protect multiple variables and compound operations Usually targets a specific variable or a limited set of states
Thread waiting May block, and may also spin The implementation may retry or use internal locks
Implementation Lock state, scheduling, and memory synchronization Atomic instructions, CAS, and so on
Programming complexity Lock scope and lifetime must be managed Complex state updates may require retry logic
Common uses Multi-field state, transaction-style business operations Counters, flags, simple state updates

1. Locks Suit Complex Operations

For example, withdrawing from a bank account:

class Account {
    private int balance = 1000;

    public synchronized boolean withdraw(int amount) {
        if (balance < amount) {
            return false;
        }

        balance -= amount;
        return true;
    }
}

Here the balance check and the balance deduction must be protected as a single unit.

If you use only two separate atomic operations, a logical race can still occur.

2. Atomic Operations Suit Simple State Updates

For example, a shared counter:

AtomicInteger counter = new AtomicInteger(0);

counter.incrementAndGet();

For simple single-variable updates, atomic classes are usually more direct than manually managed mutual exclusion locks.

3. The Two Are Not Complete Opposites

In real systems, a lock may itself use atomic operations internally to maintain its lock state.

For example:

  • Using CAS to contend for a lock.
  • Using atomic variables to maintain a wait queue.
  • Using memory barriers to establish synchronization relationships.
  • Suspending threads through operating-system wait mechanisms when contention is heavy.

So locks and atomic operations are not two completely independent low-level techniques. They solve concurrency problems at different levels of abstraction.

7. Atomicity, Visibility, and Ordering

To understand locks and atomic operations, you also need to distinguish three core concepts in concurrent programming.

1. Atomicity

Atomicity concerns whether an operation has indivisible execution semantics.

For example:

AtomicInteger count = new AtomicInteger(0);
count.incrementAndGet();

The increment operation has atomic update semantics.

But the following operation:

if (count.get() > 0) {
    count.decrementAndGet();
}

is not atomic as a whole.

Because the read and the decrement are two separate operations, another thread may change the count in between.

2. Visibility

Visibility concerns whether a modification made by one thread can be observed by other threads as specified by the memory model.

For example, in Java:

volatile boolean running = true;

After one thread modifies running, other threads reading that variable obtain the visibility guarantees specified by the Java memory model.

But volatile does not automatically make compound operations atomic.

3. Ordering

Ordering concerns whether the execution order of operations meets the requirements of the program and the memory model.

Compilers and CPUs may reorder instructions, but they must not violate the synchronization constraints specified by the language's memory model.

For example, in Java, there is a Happens-Before relationship between unlocking a given monitor lock and a subsequent lock of the same monitor.

4. Relationship Among the Three

Concept Problem Solved Common Mechanisms
Atomicity Whether an operation is indivisible Locks, CAS, atomic instructions
Visibility Whether modifications can be observed by other threads Locks, volatile, atomic variables
Ordering Whether operations satisfy necessary ordering constraints Locks, volatile, memory barriers

These three concepts are interrelated, but they cannot substitute for one another.

For example, Java's volatile can provide visibility and specific ordering guarantees, but it cannot guarantee that a compound operation such as count++ is atomic.

8. Underlying Implementation of Locks and Atomic Operations

Locks and atomic operations usually rely on atomic instructions and memory barriers provided by the processor, as well as the operating system's thread scheduling capabilities.

1. CPU Atomic Instructions

Modern processors usually provide dedicated atomic operation instructions or instruction combinations.

For example:

  • x86 instructions related to the LOCK prefix.
  • The ARM Load-Exclusive / Store-Exclusive instruction family.
  • Some atomic read-modify-write instructions in ARM.
  • Compare-and-swap or other atomic read-modify-write capabilities provided by the processor.

These mechanisms can support the implementation of low-level atomic operations.

However, different processor architectures have different instructions and memory ordering guarantees, so one cannot simply assume that all platforms implement CAS through the same kind of instruction.

2. Memory Barriers (Memory Barrier)

Memory barriers are used to constrain the reordering of specific memory operations and help implement the synchronization semantics required by the language memory model.

Common concepts include:

Barrier Type Main Function
Load Barrier Establishes ordering constraints on read operations
Store Barrier Establishes ordering constraints on write operations
Full Barrier Establishes stronger ordering constraints on read and write operations

Actual instructions and barrier semantics depend on the processor architecture and language runtime.

Programmers should generally prefer synchronization primitives provided by the language rather than writing low-level barrier code themselves.

3. Underlying Implementation of Locks

Taking a common mutex lock as an example, its internals may involve:

  • Atomic variables maintaining lock state.
  • CAS to compete for the lock.
  • Maintaining a queue of waiting threads.
  • Spinning or blocking after contention fails.
  • Waking up waiting threads after the lock is released.

Not all locks necessarily use operating system mutexes, and not all lock contention immediately causes threads to enter kernel mode.

4. Underlying Relationship Between Locks and Atomic Operations

flowchart TD
    A["Concurrency synchronization requirements"] --> B["Lock mechanisms"]
    A --> C["Atomic operations"]
    B --> D["Atomic state updates"]
    B --> E["Wait queues and thread scheduling"]
    B --> F["Memory synchronization constraints"]
    C --> G["CPU atomic instructions"]
    C --> H["CAS or atomic read-modify-write"]
    C --> I["Memory ordering constraints"]
    D --> J["Implement mutual exclusion and state management"]
    E --> J
    F --> K["Establish correct synchronization relationships"]
    G --> L["Perform indivisible updates"]
    H --> L
    I --> K

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
    style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
    style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
    style I fill:#E8EDF2,stroke:#B8C3CE,color:#334155
    style J fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style K fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style L fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    linkStyle default stroke:#A3B8AC,stroke-width:1.5px