Skip to main content
Be Bitwise

What Is Concurrency?

Concurrency & Race Conditions · VR Foundations track

3 min read

In this module

Running Things at the Same Time

Back in Process Management & Scheduling, you learned that the OS rapidly switches between processes, giving each a slice of CPU time. That's concurrency — multiple tasks making progress within overlapping time periods. On a single-core CPU, only one thread executes at any instant, but the switches happen fast enough to feel simultaneous. On a multi-core CPU, threads can run at the exact same instant on different cores — that's parallelism, a stronger claim.

Threads vs Processes

A process is an isolated program with its own virtual address space (Memory Fundamentals: Virtual Memory). A thread is a lighter-weight execution unit that shares its process's memory. Shared address space means threads can read and write the same variables — their greatest strength and their greatest hazard, often at the same time.

When Thread A writes to a shared variable and Thread B reads it, the result depends on ordering. On a single-core system, the scheduler decides. On multi-core, both might fire simultaneously on different cores. Either way, the programmer can't predict the exact operation order.

Non-Determinism

Concurrent programs are non-deterministic. Running the same program with the same inputs can produce different results depending on how the OS schedules threads. A program that works correctly 999 times out of 1,000 might fail catastrophically on the 1,000th run because the threads happened to interleave differently.

The Shared Memory Model

Modern CPUs add another layer of complexity: each core has its own cache. When Core 0 writes a value, Core 1 might still see the old cached copy. Hardware cache coherence protocols (like MESI) exist to keep caches synchronised, but there are still subtle ordering guarantees that vary between architectures. x86 provides relatively strong ordering, while ARM allows more reordering for performance.

Even x86's "strong" ordering has a gap. Each core parks its stores in a private store buffer before they reach cache, so a core can read a value before its own earlier store is visible to anyone else. The stepper below is the classic two-thread test of that. Start in sequential consistency and try to finish with both registers at 0; you can't. Switch the model to x86-TSO, leave both stores sitting in their buffers, and run the loads.

Store buffering (SB litmus test)

T1 writes x then reads y; T2 writes y then reads x. Under sequential consistency at least one load must see a 1. Under x86-TSO each store waits in its thread's store buffer until it is flushed to memory.

Memory model
T1
  1. 0x = 1;
  2. 1r1 = y;
Registers
  • r1 = 0
T2
  1. 0y = 1;
  2. 1r2 = x;
Registers
  • r2 = 0
Shared memory
  • x = 0
  • y = 0
0 steps
Every possible schedule

0 of 6 possible schedules end with r1 = r2 = 0.

Final resultSchedules
r1 = 1, r2 = 14
r1 = 0, r2 = 11
r1 = 1, r2 = 01

ARM is weaker still and allows more reorderings than this one. Memory Ordering and Barriers picks the topic up properly.

Why Concurrency Is Hard

The fundamental problem is that the human brain thinks sequentially. We read code top to bottom and expect that order. Concurrent code has no single order — every possible interleaving of thread operations is a valid execution. Two threads running just three shared-memory operations each can interleave in twenty different orders. A real program has billions. Testing can't cover them all. Concurrency bugs hide in rare timing windows, sometimes for years, then detonate in production at 3am on a Saturday.