Lesson 2 of 9 · 23m
Threads: Shared Memory, Shared Fate
Understand what Linux threads share, what they keep private, and why synchronization failures corrupt state long before they crash cleanly.
Threads are often introduced as lightweight processes. That phrase hides the operational truth: threads are separate flows of execution sharing one memory image and most kernel resources. They are cheap compared with full process isolation because they give up isolation. Every race, use-after-free, and accidental file-descriptor handoff is payment for that sharing.
Linux blurs the implementation line
On Linux, pthreads are created with the clone() syscall using flags that select what is shared:
CLONE_VM share memory mappings
CLONE_FILES share the file descriptor table
CLONE_FS share root and current working directory
CLONE_SIGHAND share signal handlers
CLONE_THREAD place in the same thread groupUsers see one PID for the thread group and different thread IDs inside it. ps -L makes that visible:
python3 - <<'PY' &
import threading, time
threading.Thread(target=lambda: time.sleep(60), daemon=True).start()
time.sleep(60)
PY
pid=$!
ps -o pid,tid,comm,stat -L -p "$pid"Threads share memory but not stacks or registers
Each thread has its own stack, registers, instruction pointer, thread-local storage, and scheduling state. They share the heap, globals, memory mappings, and file descriptor table. That distinction explains two classic bugs: returning a pointer to a thread-local stack object is immediately invalid, while passing a heap pointer can be valid but racy.
Data races are state corruption, not just bad timing
Consider two threads incrementing one counter:
counter++;That line is typically read, modify, write — not an atomic operation. Two threads can read the same value and overwrite each other, losing an update. The worst part is that the failure depends on timing, cache state, compiler optimization, and machine load. A test that passes a million times proves only that the timing window did not align.
The synchronization primitive must match the invariant:
mutex protect a critical section
rwlock many readers, occasional writer
atomic integer simple counters and flags
condition var wait until state changes
channel/queue hand ownership between threadsCancellation and signals are sharper than they look
Killing one thread in a shared-memory process can leave mutexes locked and shared state half-mutated. POSIX thread cancellation is dangerous unless code is written around cancellation points. Signals are process-directed or thread-directed depending on how they are generated; handlers are shared while masks are per-thread. This is why robust services prefer coordinated shutdown flags over force-killing worker threads.
Field notes
- A crash in one thread normally terminates the entire process; thread isolation does not contain memory corruption.
stracefollows syscalls, not logical application locks; combine it withgdb, core dumps, or language-level thread dumps.- Oversubscribing CPU-bound threads increases context switching and cache churn rather than increasing total work.
- File descriptors are shared, so one thread can close an fd another thread is about to use.
Was this lesson helpful?