1. What Is Thread Safety?
Thread safety means that a program, object, or function still maintains its expected behavior and data correctness when accessed concurrently by multiple threads.
When multiple threads access shared resources simultaneously without proper synchronization, problems such as data overwrites, inconsistent state, and incorrect results can occur.
For example, two threads simultaneously performing an increment operation on a shared counter:
class Counter {
int count = 0;
void increment() {
count++;
}
}
On the surface, count++ looks like a simple operation, but it typically consists of multiple steps:
- Read the current count value.
- Add one to the count value.
- Write the new value back to memory.
Suppose two threads both read count = 0:
- Thread A reads 0.
- Thread B reads 0.
- Thread A computes 1.
- Thread B computes 1.
- Thread A writes 1.
- Thread B writes 1.
The final result is 1, not the expected 2.
This situation is called a lost update, and it is a common data race problem in concurrent programs.
Core Goals of Thread Safety
Thread safety generally needs to guarantee the following properties:
| Property | Meaning |
|---|---|
| Atomicity | Critical operations must not expose intermediate states to other threads |
| Visibility | A thread's modifications can be observed by other threads as specified by the memory model |
| Ordering | The execution order of operations conforms to program requirements and memory model constraints |
| State consistency | Concurrent access does not violate the invariants of an object or data structure |
| Thread isolation | Operations from different threads do not interfere in unexpected ways |
Note that atomicity, visibility, and ordering are the three core concepts commonly used in concurrent programming, but they cannot individually cover all thread safety issues. For example, even if each individual operation is atomic, a combination of multiple operations can still produce logical errors.
2. What Is a Race Condition?
A race condition means that the correct result of a program depends on the uncontrollable execution timing among multiple execution units, and changes in that timing can lead to incorrect results.
When multiple threads access shared data simultaneously, the program may produce different results depending on the execution order.
For example:
class BankAccount {
int balance = 1000;
void withdraw(int amount) {
if (balance >= amount) {
balance -= amount;
}
}
}
Suppose the account balance is 1000, and two threads simultaneously withdraw 800.
The following execution order may occur:
| Step | Thread A | Thread B | Balance |
|---|---|---|---|
| 1 | Read balance 1000 | 1000 | |
| 2 | Read balance 1000 | 1000 | |
| 3 | Check balance is sufficient | 1000 | |
| 4 | Check balance is sufficient | 1000 | |
| 5 | Write balance 200 | 200 | |
| 6 | Write balance 200 | 200 |
The final balance is 200, but two withdrawals of 800 actually occurred, and from a business perspective one of them should have been rejected.
The problem is not the subtraction itself, but the fact that the balance check and the balance update are not an indivisible business operation.
Conditions for Race Conditions
Race conditions typically involve the following factors:
- Multiple threads or execution units participate in the operation simultaneously.
- They access common state or resources.
- At least one execution unit modifies that state.
- There is insufficient synchronization or coordination between the operations.
- Changes in execution order can affect the program's results.
Race conditions do not necessarily occur every time. They often depend on specific scheduling timing, so they may remain unreproducible in a test environment for a long time while occurring sporadically in production.
3. The Difference Between Data Races and Race Conditions
Data race and race condition are often used interchangeably, but they are not entirely the same.
1. Data Race
A data race typically refers to multiple execution threads concurrently accessing the same memory location, where at least one access is a write operation, and there is no synchronization relationship between the accesses that satisfies the requirements of the language's memory model.
For example:
```cpp #includeIn C++, two threads concurrently reading and writing a plain non-atomic variable counter constitutes a data race, and the program behavior is undefined (Undefined Behavior).
Here you cannot simply assume the result is necessarily 1 or 2. For a C++ program with a data race, compiler optimizations and the language memory model can both cause behavior to deviate from intuitive expectations.
2. Race Condition
Race conditions have a broader scope.
They focus on: whether the correctness of the program depends on execution timing.
Race conditions can arise from memory data races, or from logical timing issues between multiple operations.
For example:
if (!file.exists()) {
createFile();
}
Two threads may simultaneously determine that the file does not exist, and then simultaneously attempt to create it.
Even if the file system operation itself has a certain atomicity, the check and the create are still two independent operations, and the combination contains a race window.
This type of problem is commonly called a race between check and execution (TOCTOU, Time Of Check To Time Of Use).
3. Differences Between the Two
| Comparison Dimension | Data Race | Race Condition |
|---|---|---|
| Focus | Memory access and synchronization relationships | Impact of execution timing on results |
| Whether shared data is involved | Usually involves shared memory locations | Not necessarily limited to memory variables |
| Whether a write operation must be included | Usually at least one write operation | Not necessarily manifested as ordinary memory read/write conflicts |
| Whether it may cause undefined behavior | Depends on the language memory model; in C++ a data race causes undefined behavior | Not necessarily |
| Typical problems | Reading/writing shared variables without synchronization | Duplicate deductions, duplicate creation, failed state checks |
It can be understood this way:
- A data race is a specific type of concurrent access problem.
- A race condition is a broader class of timing-dependent problems.
- A data race may lead to a race condition, but a race condition does not necessarily involve a data race.
IV. Common Types of Race Conditions
1. Lost Update
Multiple threads simultaneously read the same old value, each compute a new value, and when writing back, they overwrite each other.
counter++;
When two threads execute the increment simultaneously, one of the updates may be lost.
Typical scenarios:
- Account balance updates.
- Inventory quantity modifications.
- Shared counters.
- Statistical data accumulation.
2. Dirty Reads or Intermediate State Reads
One thread is modifying multiple related fields, and another thread reads part of the state before the modification is complete.
For example:
class User {
String name;
int age;
}
When thread A updates user information:
user.name = "Alice";
user.age = 30;
Thread B may read data between the two assignments, obtaining the new name and the old age.
If the business logic requires the two fields to remain consistent, the entire state update process needs to be protected by synchronization.
3. Check-and-Act Race (TOCTOU)
The program first checks a certain condition, and then performs an operation based on the check result.
If the shared state changes between the two operations, the previous check result may become invalid.
For example:
if path.exists():
with open(path, "w") as file:
file.write("data")
The file state may change between the check and the open.
For file operations, mechanisms such as atomic creation and exclusive opening provided by the operating system should generally be preferred, rather than relying on check-then-act.
4. Initialization Race
Multiple threads simultaneously discover that an object has not yet been initialized, and then repeatedly execute the initialization logic.
if (instance == null) {
instance = new Service();
}
Two threads may simultaneously pass the instance == null check, thereby creating two instances.
For shared resources such as singleton objects, connection pools, and configuration loaders, the initialization process needs proper synchronization.
5. Task Completion Order Race
Multiple asynchronous tasks execute simultaneously, and their completion order differs from their start order.
For example:
async function main() {
const a = fetchDataA();
const b = fetchDataB();
const resultA = await a;
const resultB = await b;
}
The two requests may complete in any order.
If a later-completing old request overwrites the result of an earlier-completing new request, problems such as UI state regression may occur.
These issues are not necessarily memory data races in the traditional sense, but they still count as concurrency problems that require managing execution ordering.
V. Implementation Mechanisms for Thread Safety
To solve thread-safety problems, the core is to control how shared state is accessed and to ensure that multiple execution units coordinate correctly with one another.
1. Mutex
A mutex is used to protect a critical section, ensuring that only one thread can hold the lock and execute the protected code at any given time.
#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 is used to protect the shared variable, and std::lock_guard automatically manages the lock's lifetime.
The core idea is:
- Acquire the lock before entering the critical section.
- After successfully acquiring it, perform operations on the shared data.
- Release the lock when leaving the scope.
- Other threads wait for the lock to be released before entering the critical section.
A mutex can protect compound operations, not just reads and writes of a single variable.
2. Execution Flow with Lock Protection
flowchart TD
A["A thread requests access to a shared resource"] --> B["Attempt to acquire the mutex"]
B --> C{"Acquired successfully?"}
C -->|"No"| D["Wait for the lock to be released"]
D --> B
C -->|"Yes"| E["Enter the critical section"]
E --> F["Read or modify shared data"]
F --> G["Exit the critical section"]
G --> H["Release the mutex"]
H --> I["Other threads can 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
When using a mutex, note the following:
- The lock's scope should cover the complete operation on shared state.
- Do not perform unnecessary time-consuming operations while holding the lock.
- All code that accesses the same protected state must follow the same locking protocol.
- You must ensure that the lock can still be released when an exception occurs or a function returns early.
3. Read-Write Lock
A read-write lock allows multiple threads to read shared data at the same time, but write operations require exclusive access.
Typical rules:
- Multiple reader threads can hold read locks simultaneously.
- A writer thread needs an exclusive write lock.
- A write lock generally cannot be held at the same time as other read locks or write locks.
- The specific fairness and read/write priority policies depend on the implementation.
It is suitable for scenarios where read operations greatly outnumber write operations, for example:
- Configuration reads.
- Cache lookups.
- Metadata access.
- Read-only indexes.
However, a read-write lock is not necessarily faster than a mutex. Its state management is more complex, which may introduce additional overhead and may also cause writer starvation.
4. Atomic Operations
Atomic operations can guarantee that a specific operation has indivisible semantics in a concurrent environment.
For example, in C++:
#include <atomic>
#include <thread>
#include <iostream>
std::atomic<int> counter{0};
void increment() {
counter.fetch_add(1);
}
int main() {
std::thread t1(increment);
std::thread t2(increment);
t1.join();
t2.join();
std::cout << counter.load() << std::endl;
}
fetch_add() can atomically perform the read, increment, and write-back.
Atomic operations are usually suitable for:
- Counters.
- Flags.
- State transitions.
- Simple shared statistics.
- Some low-level operations in lock-free data structures.
However, atomic variables do not mean that all compound business logic is automatically safe.
For example:
if (balance.get() >= amount) {
balance.addAndGet(-amount);
}
Even though get() and addAndGet() are atomic operations respectively, the entire check-and-deduct process is still not atomic.
A CAS loop can be used:
import java.util.concurrent.atomic.AtomicInteger;
class Account {
private final AtomicInteger balance = new AtomicInteger(1000);
boolean withdraw(int amount) {
while (true) {
int current = balance.get();
if (current < amount) {
return false;
}
int next = current - amount;
if (balance.compareAndSet(current, next)) {
return true;
}
}
}
}
Here, compare-and-swap (Compare-And-Swap, CAS) ensures that the balance check and update are atomic.
5. CAS (Compare-And-Swap)
CAS is a common atomic update mechanism.
It usually includes three core parameters:
- V: the current value in memory.
- A: the expected old value.
- B: the new value to be written.
Execution rules:
- If V equals A, update V to B.
- If V does not equal A, the update fails.
- Return whether the operation succeeded, so the caller can decide whether to retry.
The basic CAS flow:
flowchart TD
A["Read current value V"] --> B["Calculate new value B"]
B --> C["Execute CAS"]
C --> D{"Is the current value equal to the expected value A?"}
D -->|"Yes"| E["Write new value B"]
E --> F["Operation succeeded"]
D -->|"No"| G["Update failed"]
G --> H["Re-read 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
CAS's advantage is that it avoids the explicit blocking of traditional mutual exclusion locks, but it also has limitations:
- Under high contention, it may produce many retries.
- Complex state updates may be difficult to express.
- Attention must be paid to the ABA problem.
- Atomicity, memory visibility, and memory ordering are guarantees at different levels.
6. Immutable Object
Once an immutable object is created, its internal state cannot be modified.
If multiple threads only read an immutable object, no mutex needs to be set for modifications to the object's state, because there are no concurrent modifications.
For example:
public final class User {
private final String name;
private final int age;
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Advantages of immutable objects:
- There is no concurrent modification of the object's internal state.
- They are easier to share.
- They are easier to test and reason about.
- They can reduce the use of locks.
- They help build functional concurrent programs.
However, immutable objects require attention to deep immutability. If the object internally holds mutable collections or mutable references, merely declaring fields as final does not guarantee that the entire object is completely immutable.
VI. Memory Visibility and the Memory Model
Thread safety involves not only atomicity, but also whether one thread's modifications can be correctly observed by another thread.
1. What Is Memory Visibility?
Modern CPUs and compilers perform caching, instruction reordering, and optimization to improve execution efficiency.
Therefore, after one thread modifies a shared variable, another thread may not immediately observe the modification.
For example, in Java:
class Worker {
boolean running = true;
void work() {
while (running) {
// Execute task
}
}
void stop() {
running = false;
}
}
Without a correct synchronization relationship, the worker thread may not observe changes to running as expected.
One solution is to use volatile:
class Worker {
volatile boolean running = true;
void work() {
while (running) {
// Execute task
}
}
void stop() {
running = false;
}
}
Java's volatile can provide visibility and certain ordering guarantees for that variable, but it cannot guarantee atomicity for compound operations.
For example:
volatile int count = 0;
count++;
count++ is still not an atomic operation.
2. Happens-Before Relationship
Happens-Before is an important concept in concurrent memory models, used to describe the visibility and ordering constraints that operations must satisfy.
If operation A Happens-Before operation B, then:
- The effects of A must be visible to B as specified by the memory model.
- There is a corresponding ordering constraint between A and B.
- Compilers and processors must not optimize in a way that breaks this relationship.
Common Happens-Before rules in Java include:
| Rule | Meaning |
|---|---|
| Program order rule | In the same thread, an operation earlier in program order Happens-Before a later operation |
| Monitor lock rule | An unlock on the same lock Happens-Before a subsequent lock |
| volatile rule | A write to a volatile variable Happens-Before a subsequent read of that variable |
| Thread start rule | A call to start() on a thread Happens-Before operations in the started thread |
| Thread termination rule | Operations in a thread Happens-Before another thread successfully detects that the thread has terminated |
| Transitivity rule | A Happens-Before B, B Happens-Before C, then A Happens-Before C |
These rules are specific rules in the Java Memory Model and should not be directly treated as a unified memory model for all languages.
3. Illustration of Memory Visibility
flowchart TD
A["Thread A"] --> B["Write shared variable"]
B --> C["Release synchronization lock"]
C --> D["Establish Happens-Before relationship"]
E["Thread B"] --> F["Acquire the same synchronization lock"]
F --> G["Read shared variable"]
D --> G
G --> H["Observe the modification result consistent with the memory model"]
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:#E0F0FE,stroke:#93BCE8,color:#1E4976
style F fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
The key here is not simply assuming that “after writing to main memory, other threads can read it,” but rather establishing correct visibility guarantees through the synchronization relationships specified by the language memory model.
VII. Common Problems with Locks
Locks can solve shared-state access problems, but improper use can also introduce new concurrency faults.
1. Deadlock
Deadlock refers to multiple threads waiting for resources held by each other, causing all involved threads to be unable to continue execution.
For example:
synchronized (lockA) {
synchronized (lockB) {
// Operation
}
}
Another thread:
synchronized (lockB) {
synchronized (lockA) {
// Operation
}
}
The following may occur:
- Thread A holds
lockAand waits forlockB. - Thread B holds
lockBand waits forlockA. - Neither thread can continue.
Deadlock usually requires four conditions to be satisfied simultaneously:
- Mutual exclusion: a resource cannot be occupied by multiple threads at the same time.
- Hold and wait: a thread holds resources while waiting for other resources.
- No preemption: resources cannot be forcibly preempted.
- Circular wait: threads form a circular resource dependency.
Common ways to avoid deadlock:
- Use a consistent order for acquiring locks.
- Reduce nested locks.
- Shrink critical sections.
- Use lock acquisition mechanisms with timeouts.
- Avoid performing external calls while holding locks.
2. Livelock
Livelock means that threads are not blocked, but because they continuously respond to each other's state changes, they can never complete actual work.
For example, two threads continuously detect a conflict and voluntarily yield, then collide again on the next attempt.
Differences between livelock and deadlock:
| Aspect | Deadlock | Livelock |
|---|---|---|
| Thread state | Blocked waiting | Continuously running |
| CPU usage | The relevant threads usually no longer perform useful work | May continue to consume CPU |
| State changes | Usually no progress | Constantly changing but no effective progress |
| Resolution approach | Break the circular wait | Introduce random backoff and adjust retry strategies |