Below is an in-depth look at “what a thread really is” under the hood—from the OS’s point of view through to user-level libraries, scheduling, context-switching, stacks, synchronization and common pitfalls.
1. Threads vs. Processes
Process
Has its own virtual address space, file descriptors, signal handlers, etc.
Heavyweight: creating a process can be costly (fork, exec).
Thread
Sometimes called a “lightweight process.”
Shares most of its context with sibling threads in the same process: address space, global/static variables, open files.
Has its own:
• Thread Control Block (TCB)
• Stack (for local variables & function calls)
• CPU register context (when it’s not running)
2. Thread Anatomy
Thread Control Block (TCB)
Thread ID, state (Ready, Running, Blocked, Terminated)
CPU registers (saved when preempted)
Stack pointer & instruction pointer
Scheduling priority, affinity, accounting info
Stack
Each thread gets its own stack memory region (size often set at creation).
Deep recursion or large stack-allocated arrays can cause stack overflows.
Kernel vs. User Threads
Kernel thread: the OS scheduler knows about it directly.
User thread (green thread/fiber): managed entirely in user space by a runtime (e.g. Go routines, Java virtual threads).
• Very fast switching (no syscall)
• But if one blocks in a syscall, it can block the entire OS thread unless the runtime schedules another.
Creation cost:
• User-space threads: just a few pointer ops & stack alloc
• Kernel threads: syscall, TCB setup, scheduler queues
Destruction: must free stack + TCB; OR join/wait to retrieve exit code.
4. Scheduling & Context Switch
Preemptive scheduler (most general-purpose OSes)
Each thread gets a quantum (time slice).
On timer interrupt: current registers are saved into its TCB; a new thread’s registers are loaded.
Context switch overhead
Saving/restoring ~ a few dozen registers
Flushing pipeline, TLB shoot-downs if core affinity changes
A microsecond to tens of microseconds, depending on hardware & OS
Affinity & load balancing
Threads can be pinned (affinity) to certain cores
Kernel attempts to balance runnable threads across cores
5. Synchronization Primitives
Because threads share memory, we need primitives to ensure correctness:
Mutexes (Locks)
• Guarantee mutual exclusion over critical section
• Can be implemented with atomic instructions (e.g. compare-and-swap) + queue
Condition variables
• Wait/signal mechanism to synchronize phases
Semaphores
• Counting semaphore to allow up to N threads in a section
Futures/Promises, Barriers
• Higher-level sync or coordination abstractions
Lock-free/wait-free data structures
• Rely on atomics to avoid blocking entirely
Pitfalls:
– Deadlock: circular wait on locks
– Priority inversion: low-priority thread holds lock needed by high-priority
– Starvation: threads never get CPU or lock
– Race conditions: missing synchronization
6. User-Level vs. Kernel-Level Threads
Feature
Kernel Thread
User Thread (Green)
Scheduling
By OS scheduler
By user-level runtime
Context switch cost
High (syscall)
Low (function call)
True parallelism
Yes, on multiple cores
No, bound to one OS thread
Blocking syscalls
Blocks whole OS thread
Usually blocks all user threads on that OS thread
Languages/runtimes often mix both. E.g. Go maps many goroutines (user) onto a smaller pool of OS threads.
7. Memory Model & Visibility
Threads must agree on how memory operations become visible to each other. Most languages/architectures define:
Sequential consistency (ideal but slow)
Relaxed/weak consistency (x86 TSO, ARM weakly ordered)
Memory barriers/fences to enforce ordering
In Java/C++11 you have volatile, atomic<>, memory orders (acquire/release), etc.