
Threads, Mutexes, and Atomics: Start with What Can Go Wrong
How threads share memory, what a mutex actually does on Linux, and why replacing locks with atomics is not automatically an optimization.
以下正文同步自 Hacker News 最新,版权归原站所有,已转换为易读排版。
How threads share memory, what a mutex actually does on Linux, and why replacing locks with atomics is not automatically an optimization.
Ted Koomen · October 30, 2026 · 18 min read
This series is about inter-thread communication and synchronization: how threads share work, how they get in each other's way, and what we can do about it.
I want to get past “put a mutex here” and “atomics are faster.” We will look at what the code guarantees, what the operating system does, and what is happening on the CPU. Those are related questions, but they are not interchangeable.
This first post starts with processes and threads, then works through mutexes, atomics, memory ordering, and a few ways all of this can go wrong. The next part will put those ideas into actual implementations and measure them. Adding threads does not, by itself, make a program faster.
The examples use C++20. You can run the working examples below and inspect their x86-64 assembly through Compiler Explorer. The deliberately broken examples are compile-only. Printing happens outside the worker hot paths so it does not introduce an extra shared I/O bottleneck. The implementation discussion focuses on Linux, with some x86 and Arm details where they matter.
A process is a running instance of a program. It has a virtual address space and resources managed by the operating system: open files, memory mappings, and so on.
A thread is a path of execution inside that process. Each thread has its own execution state, including registers, a stack, and thread-local storage. Threads in the same process share the address space, so they can access the same heap objects and global variables.
“Its own stack” does not mean that stack is protected from the other threads. If you pass another thread a pointer to a local variable, it can access that memory. Keeping the object alive and coordinating access are still your responsibility.
That shared address space is both the appeal and the problem. Passing data to another thread can be as simple as handing it a pointer. Making sure the other thread sees valid data, at the right time, is where the work starts.
The word thread also gets used for hardware, which makes this more confusing than it needs to be.
An OS thread is something the scheduler can run. A hardware thread is an execution context exposed by the processor, often called a logical CPU. With simultaneous multithreading, or SMT, a physical core exposes multiple logical CPUs that share many of the core's resources. Hyper-Threading is Intel's name for its SMT implementation.
The operating system schedules runnable software threads onto those logical CPUs, subject to affinity and other scheduling constraints. You can have far more software threads than logical CPUs; they just cannot all execute at once.
Two SMT siblings are not two full cores. They share execution resources, and running work on both can help utilization or create contention. Whether it helps your workload is something to measure, especially if you care about latency.
Linux represents schedulable threads as tasks. Each has a thread ID, or TID. Threads in the same process belong to a thread group and share its thread-group ID, which is what getpid() returns.
The clone() and clone3() interfaces let the caller specify which resources a new task shares. For example, CLONE_VM shares the address space, CLONE_FILES shares the file-descriptor table, and CLONE_THREAD places the new task in the same thread group. Other flags and setup handle things such as signal handlers, thread-local storage, and thread termination.
So yes, processes and threads are closely related in Linux. But a short clone(flags, ...) expression is not a complete thread implementation, and it is not the API I would reach for in ordinary C++. Use std::thread, std::jthread, or the platform thread library and let it handle the machinery. The Linux clone documentation is useful if you want to look underneath that abstraction.
Threads are useful when work can overlap. A server might have one thread waiting for input while workers process requests. Independent CPU work can run on separate cores. A background task can keep a user-facing thread responsive.
None of that guarantees a speedup. If every worker immediately queues behind the same mutex, adding workers mostly gives you a longer queue. Thread creation, scheduling, synchronization, cache traffic, and memory bandwidth all cost something. Async I/O is another option when the real problem is waiting on lots of connections rather than doing CPU work.
I would generally consider threads when tasks need to share substantial in-memory state and belong in the same failure domain. An unhandled fatal fault in one thread will normally take down the process. There is no isolation boundary between your parser thread and your order-processing thread just because they have different names.
Separate processes give you stronger isolation. Their address spaces are separate by default, which helps contain accidental memory corruption and lets you restart components independently. They can also run with different permissions.
Communication between processes can involve system calls and copying, but it does not have to copy every payload. Shared-memory IPC lets processes access the same mapped storage. At that point, you have brought back many of the synchronization problems you were trying to separate, along with questions about what happens if a process dies.
Pick the ownership and failure boundaries first. Then decide how the work should execute and communicate.
Here is a very small program with a real correctness problem:
#include <iostream> #include <thread> #include <vector>
int count = 0;
void increment_count() { ++count; }
int main() { std::vectorstd::thread threads; threads.reserve(10);
for (int i = 0; i < 10; ++i) { threads.emplace_back(increment_count); } for (auto& thread : threads) { thread.join(); }
std::cout << "Final count: " << count << '\n'; }
Compile-only example: this program deliberately has undefined behavior.
++count looks like one operation in the source. That does not make it an atomic operation between threads.
One way to picture the failure is two threads reading the same value, both adding one, and both writing the same result. An increment disappears. That is a useful illustration, but it is not the full C++ rule.
These are conflicting non-atomic accesses with no happens-before relationship between them. That is a data race, and a C++ data race is undefined behavior. The language does not promise that the only possible outcome is a slightly wrong count. It does not promise a particular range of results at all.
The program might print 10 every time you try it. That would not make it correct. Joining the workers makes the final read safe relative to their completion; it does not fix the unsynchronized accesses between the workers.
“Race condition” is a broader term for correctness depending on timing. A data race is a specific language-level problem. You can eliminate data races with atomics and still have a broken algorithm.
A mutex gives one thread ownership at a time. Every access that needs protection must follow the same locking rule. The mutex does not inspect your variables or stop another thread from ignoring it.
#include <iostream> #include <mutex> #include <thread> #include <vector>
int count = 0; std::mutex mtx;
void increment_count() { std::lock_guardstd::mutex lock(mtx); ++count; }
int main() { std::vectorstd::thread threads; threads.reserve(10);
for (int i = 0; i < 10; ++i) { threads.emplace_back(increment_count); } for (auto& thread : threads) { thread.join(); }
std::cout << "Final count: " << count << '\n'; }
Runnable C++20 example. Compile locally with g++ -std=c++20 -O2 -pthread example.cpp -latomic.
std::lock_guard acquires the mutex when it is constructed and releases it when it is destroyed. That includes leaving the scope through normal exception unwinding. I do not want every return path to depend on somebody remembering to call unlock().
Now only one worker can execute the increment at a time. Unlocking the mutex also synchronizes with a subsequent successful lock of that mutex, so the next owner can observe the previous owner's protected writes.
This makes the critical section exclusive relative to other code taking the same mutex. It does not turn the whole function into one CPU instruction. After all ten workers have joined, the count is 10.
How two threads take turns with a mutex. A locks the mutex. B attempts to lock it and waits. A increments the counter from 0 to 1, then unlocks. B can acquire the mutex, increment from 1 to 2, and unlock. Unlocking makes acquisition possible; it does not guarantee an immediate handoff.
#include <iostream> #include <mutex>
struct Oops { void dead() { std::lock_guardstd::mutex lock(mtx); lock_again(); }
void lock_again() { std::lock_guardstd::mutex lock(mtx); }
std::mutex mtx; };
int main() { Oops o; std::cout << "About to re-lock a mutex owned by this thread: undefined behavior." << std::endl; o.dead(); }
Compile-only example: this program deliberately has undefined behavior.
The thread already owns mtx when lock_again() tries to acquire it.
With std::mutex, that violates the locking precondition and gives you undefined behavior. A self-deadlock is a common result, but “this must block forever” is not a portable guarantee. An implementation may detect the problem instead. The C++ mutex requirements spell out that precondition.
A different deadlock involves two threads and two mutexes: A owns the first and waits for the second, while B owns the second and waits for the first. Neither can reach its unlock. Consistent lock ordering helps; when you need to acquire multiple mutexes together, std::scoped_lock provides deadlock avoidance for that acquisition. It cannot fix every cycle elsewhere in your program.
std::recursive_mutex allows the owning thread to lock it again. Each successful lock needs a matching unlock; another thread cannot acquire it until the owner has released every level.
It solves that specific re-entry problem. It does not solve deadlocks between different mutexes, and it does not make it safe for a nested call to observe an object halfway through an update.
When I see a recursive mutex, my first question is why the ownership boundary requires it. Sometimes there is a reasonable answer. Often I would rather have a public method that takes the lock and a private helper that assumes the caller already holds it.
Is a mutex software or hardware? It is a software abstraction whose implementation usually relies on both CPU atomic operations and operating-system support.
For a typical ordinary mutex on Linux, the uncontended path happens in user space. The library attempts to acquire the lock using atomic operations. The kernel does not run first on every lock attempt.
When the mutex is contended, the implementation may spin briefly or go to a blocking path. Common Linux implementations use a futex, short for fast user-space mutex, to let the kernel park a waiter and wake it when appropriate. The futex wait checks the expected value as part of blocking, which is important for avoiding a lost wakeup between checking the lock and going to sleep.
The blocked thread does not normally wake up on a timer just to keep checking. An unlock can trigger a wakeup, and the waiter then retries as required by the algorithm. Spurious wakeups and other returns are also possible; being woken is not the same as owning the mutex.
That distinction matters for latency. A woken thread must become runnable and get scheduled, then successfully acquire the lock. Another contender may get there first. std::mutex does not promise FIFO acquisition.
The exact behavior depends on the library, mutex type, and platform. But “every lock is a system call” is the wrong starting point. So is “a mutex
正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。