CS Fundamentals Trainer · Guides
Concurrency: races, deadlocks and the event loop
Threads, event loops and goroutines compared; the lost update and the deadlock as animations you can step through; what the fixes cost, measured in Go and Python; and the one rule of an event loop.
27 min read
Concurrency is about several things being in progress at once: a server handling a thousand requests, a program waiting on a database while it also reads a file, a UI that stays responsive while it downloads. It is also the source of the bugs people find hardest to reproduce, because the program is correct on most runs and wrong on a few. This guide builds the vocabulary (threads, event loops, goroutines), shows the two classic failures (the race condition and the deadlock) with animations you can step through, measures the fixes, and ends with the questions interviewers ask.
The numbers below come from small programs run once for this guide, on a 2-core Linux container with Go 1.24, Python 3.13 and Node 22. They are illustrations of a behaviour, not benchmarks: times and counts change from run to run and from machine to machine, and a ratio measured here does not carry over to your workload. The code for every experiment is in the appendix so you can run it yourself.
1. Concurrency, parallelism, and three models
Concurrency is structuring a program as several tasks that make progress in overlapping time. Parallelism is running several tasks at the same instant on different CPU cores. You can have concurrency without parallelism (one core switching between tasks) and parallelism is only useful if the work can be split.
Diagram: Three common ways to run many tasks at once
| Threads | Event loop | Goroutines (Go) | Actors (Erlang, Akka) | |
|---|---|---|---|---|
| Runs on | OS threads, scheduled by the OS | one thread | many lightweight tasks multiplexed over a few OS threads by the Go runtime | many lightweight processes, scheduled by the runtime |
| Shared memory | yes, by default | yes, but one callback runs at a time | yes, unless you avoid it: channels are the idiomatic way to hand data over, but nothing stops two goroutines touching one variable | no: each actor owns its state and others can only send it messages |
| Uses several cores | yes (not in CPython, see section 2) | no, unless you add workers or processes | yes | yes |
| Classic bug | data races, deadlock | blocking the loop, logical races across await | data races on shared variables, deadlock on channels and locks, leaked goroutines | a full mailbox, message ordering, deadlock through request cycles |
| Examples | Java, C# Thread, Python threading | Node.js, Python asyncio, browsers | Go | Erlang/Elixir, Akka |
The actor model is a way of organising state and communication: state is private to an actor and everything else happens through messages. Go's goroutines are a lighter kind of task that can be used in that style (Go's proverb is "do not communicate by sharing memory; share memory by communicating"), but a Go program may share memory too, and then it must synchronise it like any threaded program.
C# async/await is not the same thing as a single-threaded event loop. After an await, the rest of the method may continue on a thread-pool thread, in parallel with other code, unless the caller runs in a context that brings it back to one thread (a UI thread, classic ASP.NET). So C# code that shares data between tasks can have real data races and needs the tools of section 3. Note that lock cannot contain an await; use SemaphoreSlim (await gate.WaitAsync()) to guard an asynchronous critical section.
A task is I/O-bound when it mostly waits (network, disk, database) and CPU-bound when it mostly computes. Waiting costs nothing if the model can run something else meanwhile, which is why one Node.js process can serve thousands of connections. Computing needs cores, which is why a CPU-bound Node.js handler needs a worker thread or a separate process (section 5).
2. Race conditions: the lost update
Two terms are often used as if they were one:
- A data race is a low-level fault: two threads access the same memory at the same time without synchronisation and at least one of them writes. Languages define what such a program may do (Go's memory model and C++ treat it as a bug; Java gives weak guarantees). Race detectors look for this.
- A race condition is any bug where the result depends on the order or timing of events. It can exist without any data race: every individual memory access may be properly locked, and the program is still wrong because the sequence of steps is not protected as a whole (a check and a later update that other code can slip between). The JavaScript example below is of this kind.
Fixing one does not fix the other. A lock around each variable removes the data race but not necessarily the race condition; protecting the invariant (section 3) addresses both.
The classic form of a race is the lost update. counter += 1 looks like one step, but the machine does three: read the value, add one, write it back. If two threads interleave those steps, both read the same old value and one of the increments disappears.
Step through it below. Without the lock, both threads read 0, both compute 1, and the counter ends at 1. Tick the box to protect the update with a lock and run it again.
Interactive simulation: lost update
The same bug in real code. Four tasks each add one a million times; the answer should be 4,000,000.
counter := 0
var wg sync.WaitGroup
for w := 0; w < 4; w++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 1_000_000; i++ {
counter++ // read, add, write: not one step
}
}()
}
wg.Wait()
fmt.Println(counter)
int counter = 0;
var tasks = Enumerable.Range(0, 4).Select(_ => Task.Run(() =>
{
for (int i = 0; i < 1_000_000; i++)
counter++; // read, add, write: not one step
}));
await Task.WhenAll(tasks);
Console.WriteLine(counter);
Run three times in Go, the counter ended at 2,800,244, 2,252,535 and 2,278,252 instead of 4,000,000. That is 30 to 44 percent of the increments lost, and different every time. Go can point at the bug for you: build or run with -race (go run -race .) and the race detector prints WARNING: DATA RACE with the two conflicting accesses and the goroutines that made them. Use it in tests and CI.
The bug hides in Python (and then it does not)
In CPython the global interpreter lock (GIL) lets only one thread run Python bytecode at a time. Four threads each running counter += 1 a million times lost nothing in three runs (4,000,000 every time). That does not make the code correct: the interpreter may switch threads between bytecodes, and a free-threaded build without the GIL is available as an experimental option since Python 3.13 (PEP 703). Any pause between the read and the write exposes the problem at once:
def read_modify_write():
global counter
for _ in range(N):
value = counter # read
time.sleep(0) # any call that lets another thread run
counter = value + 1 # write back
With 4 threads and N = 200,000 (800,000 expected), the unprotected version ended at 200,004, 200,000 and 200,000, losing about three quarters of the updates: every thread read the same value in each round and all but one write was overwritten. With with lock: around the three lines the answer was exactly 800,000. The lesson: a test that passes does not prove a race is absent.
Check-then-act, with no threads at all
Race conditions are not only a threads problem. Single-threaded JavaScript has one too, because every await is a point where other code runs. There is no data race here (only one callback touches balance at any instant), but the invariant "the balance never goes below zero" is broken:
let balance = 100;
async function withdraw(amount) {
if (balance >= amount) { // check
await audit(amount); // other callbacks run here
balance -= amount; // act on information that is now stale
}
}
await Promise.all([withdraw(80), withdraw(80)]);
console.log(balance); // -60
Both calls passed the check while the balance was still 100. This is called check-then-act (or time-of-check to time-of-use), and the same shape appears in databases ("select, then update") and file systems ("does it exist? then create it"). Section 3 shows the fixes.
3. Fixing races: four tools
- Mutual exclusion (a lock, mutex). Only one task may be inside the critical section at a time. Simple and general, but it makes tasks wait and it is the cause of deadlocks (section 4).
- Atomic operations. The hardware performs read-modify-write as one indivisible step. Fast, but limited to simple values such as counters and flags.
- Message passing and ownership. Do not share the data. One task owns it and the others send it requests (a channel, a queue, an actor mailbox). Go's saying is "do not communicate by sharing memory; share memory by communicating".
- Immutability. Data that never changes cannot race. Share it freely and build a new copy to change it.
lock = threading.Lock()
def bump():
global counter
for _ in range(N):
with lock: # critical section
counter += 1
var mu sync.Mutex
mu.Lock()
counter++
mu.Unlock()
// or, for a counter, one atomic instruction:
var n atomic.Int64
n.Add(1)
private static readonly object Gate = new();
lock (Gate)
{
counter++;
}
// or, for a counter, one atomic instruction:
Interlocked.Increment(ref counter);
For JavaScript's check-then-act, make the whole check-and-act step run one caller at a time. A promise chain works as a tiny lock: with it, the two withdrawals above end at 20 (the second is refused), not -60.
let tail = Promise.resolve();
function exclusive(fn) {
const run = tail.then(fn);
tail = run.catch(() => {}); // a failure must not block the next caller
return run;
}
await Promise.all([80, 80].map((a) => exclusive(() => withdraw(a))));
What the fixes cost (one illustrative run)
The same four-goroutine, 4,000,000-increment Go program, made correct three ways. This is a single run of a micro-benchmark on a 2-core container, so read it as a picture of the shape of the trade-off. The full program is in the appendix.
| Approach | Result | Time (one run) |
|---|---|---|
| No protection | 2.3 to 2.8 million (wrong) | 3 ms |
sync.Mutex | 4,000,000 | 230 ms |
atomic.Int64 | 4,000,000 | 60 ms |
| One goroutine owns the counter, others send it messages over a buffered channel | 4,000,000 | 296 ms |
The wrong answer is fastest because nobody waits. In this run the atomic took about a quarter of the mutex's time, and the channel version was the slowest per increment. Do not turn that into a rule such as "atomics are four times faster than mutexes". The gap depends on how many tasks fight over the same variable (contention), on the CPU and its cache behaviour, on the language runtime and its lock implementation, and above all on how much work happens inside the protected section: when the section does real work, the cost of the lock itself is a small part and the atomic's advantage disappears. An atomic also cannot protect an invariant that spans several fields, which a mutex or a single owner can. A channel costs more per operation, but it moves ownership so that one goroutine can own a whole data structure without any lock.
In Python the lock made the four-thread loop slower too, by a factor that varied between roughly 2 and 4 across repeated runs, but that comparison belongs to CPython with its GIL and says little about other runtimes.
Measure your own workload, with the real amount of contention and the real work in the critical section, before choosing a tool for speed.
Rules of thumb:
- Keep critical sections small and never do slow work (a network call, a disk read) while holding a lock.
- Protect the invariant, not the variable. If two fields must change together (debit one account, credit another), one lock must cover both changes.
- Prefer the highest-level tool that fits: a counter wants an atomic, a shared cache wants a concurrent map or a mutex, a pipeline of stages wants channels or queues. Choose for correctness and clarity first, and optimise only where a measurement shows a lock is the bottleneck.
- In a database the same idea is a transaction with the right isolation level or a row lock (see transaction isolation levels).
4. Deadlock
A deadlock is two or more tasks each waiting for something the other holds, so none can ever continue. The textbook case needs two locks. Thread 1 takes A and then wants B. Thread 2 takes B and then wants A. If each gets its first lock before the other gets its second, both wait forever.
Step through it, then tick the box to make both threads take the locks in the same order.
Interactive simulation: deadlock
A deadlock needs all four of these conditions (Coffman, 1971) at once. Remove any one and it cannot happen:
| Condition | Meaning | What stops it (prevention) |
|---|---|---|
| Mutual exclusion | a resource is used by one task at a time | use immutable, read-only or lock-free data, or give each task its own copy |
| Hold and wait | a task holds one resource while waiting for another | acquire everything at once, or hold at most one lock at a time |
| No preemption | a resource cannot be taken away from its holder | rare for ordinary locks; some systems can force it (a database aborts a transaction and releases its locks) |
| Circular wait | A waits for B, B waits for A | give all locks a global order and always take them in that order |
There are three different strategies, and it helps not to mix them up:
- Prevention designs the system so that one condition can never hold. Lock ordering is the practical example: if every thread takes A before B, nobody can wait in a circle. The cheapest rule: decide an order, document it, and where the natural order is not obvious (transfer money between two accounts) order by something stable such as the account id. Also hold as few locks at once as you can, and never call unknown code (a callback, an overridable method) while holding a lock.
- Avoidance decides at run time whether granting a request could lead to a deadlock and delays it if so (the banker's algorithm). It needs to know each task's maximum needs in advance, so it is mostly a textbook idea and rare in application code.
- Detection and recovery accepts that a deadlock may happen and breaks it afterwards. A timeout on acquiring a lock (
tryLock,SemaphoreSlim.Wait(timeout), acontextdeadline) does not take the lock from its holder; it lets the waiting task give up, release what it holds, and retry or fail. That turns a permanent hang into a bounded wait, but it does not remove the cause: it can hide a design bug, it can fire wrongly when the holder is merely slow, and tasks that all back off and retry in step can livelock (add random jitter). A database does recovery for you: PostgreSQL notices a lock cycle afterdeadlock_timeout(one second by default), aborts one of the transactions withERROR: deadlock detected, and the application is expected to retry it.
Use prevention where you can, and recovery (timeouts, cancellation, rollback and retry) as a safety net, not as the design.
Go detects the simplest case at runtime. When every goroutine is blocked, the program stops with fatal error: all goroutines are asleep - deadlock! and a stack trace for each. That only fires when all of them are stuck. In a real server other goroutines keep running, so a deadlocked pair just silently stops serving its requests. Most languages give no message at all: a deadlocked service shows up as threads that never finish, a growing request queue and low CPU use. A thread dump (jstack, dotnet-dump, py-spy dump, sending SIGQUIT to a Go program) lists what each thread is waiting for and finds it.
A close relative is livelock: tasks are not blocked, but keep reacting to each other and make no progress (two people stepping aside in a corridor, both in the same direction, forever). Randomised backoff fixes it. Starvation is when a task never gets its turn because others always win the lock; fair locks and bounded queues address it.
5. The event loop and the one rule
An event loop runs one callback at a time on one thread. When a callback needs to wait (a network reply, a timer), it registers what to do next and returns, and the loop runs the next ready callback. That is why await is cheap, and why two callbacks of one event loop cannot touch the same memory at the same instant: nothing else runs until you return or await. This rules out data races between callbacks, but not race conditions: another callback can run at every await, so a check made before an await may be stale after it (the balance example in section 2). Worker threads with shared memory (SharedArrayBuffer) and runtimes whose async code spreads over several threads, such as C#, bring real data races back.
Diagram: The event loop: ready callbacks run one at a time
The price is one rule: never block the loop. A callback that computes for 500 ms, or calls a blocking function, holds up every other task, including ones that were ready long before.
In Node.js a timer asked to fire after 100 ms, while a synchronous loop kept the thread busy for 500 ms, ran after 504 ms. The same 500 ms of waiting done with await let the timer fire on time, after 100 ms.
setTimeout(() => console.log("timer"), 100);
while (performance.now() < until) {} // blocks: the timer is late
await new Promise((r) => setTimeout(r, 500)); // yields: the timer is on time
Python's asyncio behaves the same. A ticker that should print every 0.1 s was delayed by a time.sleep(0.5) call inside another coroutine: its first tick came at 0.50 s instead of 0.10 s. Replacing it with await asyncio.sleep(0.5) gave ticks at 0.10, 0.20 and 0.30 s as expected.
async def blocking():
time.sleep(0.5) # freezes every task on the loop
async def friendly():
await asyncio.sleep(0.5) # the loop runs other tasks meanwhile
Things that quietly block the loop: a CPU-heavy loop or large JSON parse, a synchronous file or database driver, requests.get in an async def, a regular expression with catastrophic backtracking. The fixes are to use the async version of the library, or to move the work off the loop: a worker thread (worker_threads, asyncio.to_thread, Task.Run) for blocking calls, a separate process for heavy computation.
A related trap in C# is async-over-sync: calling .Result or .Wait() on a task from code that runs on a context which allows only one piece of code at a time (a classic ASP.NET or UI thread). The blocked thread waits for the task, and the task needs that same thread to finish. It is a deadlock with a single lock. The fix is to use await all the way up the call chain.
6. Choosing a model
| Workload | Good fit | Why |
|---|---|---|
| Many connections, mostly waiting (web server, chat, API gateway) | event loop or goroutines | waiting is cheap; thousands of tasks, few threads |
| Heavy computation (image processing, parsing, encoding) | processes or threads on several cores | needs cores; in CPython use multiprocessing or a free-threaded build, because of the GIL |
| Blocking libraries you cannot change | thread pool | keeps the blocking off the event loop |
| Pipelines of stages | channels or queues | the data moves, nothing is shared |
Tasks are not equally expensive. In Go, 100,000 parked goroutines took about 2.0 KiB of stack each and were created in under 400 ms, so spawning one per request is normal. OS threads are heavier: each reserves its own stack (often 8 MiB of address space by default on Linux), and the scheduler has to switch between them. Starting 2,000 Python threads took 0.17 s here, and a server with tens of thousands of threads spends its time switching rather than working. That is why thread-per-request servers use a bounded pool and why event loops and goroutines exist.
Size a pool on purpose, but treat any formula as a starting estimate. For CPU-bound work a first guess is about one worker per core; more only adds switching. For work that spends time waiting, a rough estimate is cores * (1 + wait time / compute time). It assumes steady load, waits that do not depend on each other, and no limit downstream. Real systems break those assumptions: bursts make queues grow, work varies in size, a database or an API you call may allow only a fixed number of connections (which then caps the useful pool size whatever the formula says), and the runtime may behave differently (the GIL, an event loop). So start from the estimate, then load test and watch queue length and latency percentiles, and set the limit against the capacity of what the pool calls.
7. Other things that go wrong
- Unbounded concurrency. Starting a task for every item of a large list opens thousands of connections at once and overwhelms the database or the remote API. Limit it with a semaphore, a worker pool or a bounded queue, so slow consumers slow producers down (backpressure).
- Thread pool exhaustion. If every pool thread blocks waiting for a task that is queued behind them on the same pool, nothing finishes. It looks like a deadlock without any locks.
- Visibility and ordering. Without synchronisation, one thread's write may not be seen by another at all, or may be seen out of order, because of CPU caches and compiler reordering. Locks, atomics and channels also act as memory barriers that make writes visible. The Go memory model is a readable account of the rules; its advice for programs with races is "don't be clever".
- Leaks and cancellation. A task nobody waits for keeps running (and holding memory and connections) after its caller has given up. Pass a cancellation signal (
context.Contextin Go,CancellationTokenin C#,AbortControllerin JavaScript) and always know who is responsible for stopping each task. - Lost errors. An exception in a background task goes nowhere unless somebody awaits or joins it. Collect results with
Promise.all,Task.WhenAll,errgroupor equivalent. - Retries multiply load. A concurrent retry on every failure turns a slow dependency into a retry storm (see the rate limiter guide and caching strategies for stampedes).
8. Common mistakes
- Assuming
counter += 1,x = x + 1orif (not exists) createis atomic. - Trusting a test that passed: a race may need thousands of runs, or a different CPU, to show.
- Locking the variable instead of the invariant, so two related updates still interleave.
- Holding a lock across slow I/O or while calling code you do not control.
- Taking several locks without a global order.
- Blocking the event loop with CPU work or a synchronous library.
- Mixing sync and async:
.Resultand.Wait()in C#,asyncio.runinside a running loop,time.sleepin a coroutine. - Starting unlimited tasks, or tasks nobody waits for or cancels.
- "Fixing" a race with
sleep: it only moves the timing.
9. Things to mention in an interview
- Define the words: concurrency versus parallelism, I/O-bound versus CPU-bound.
- Explain a race with the three steps (read, modify, write) and give the lost update or check-then-act as the example.
- Know the fix menu: lock, atomic, message passing, immutability, and the trade-off of each.
- Name the four deadlock conditions and separate prevention (lock ordering), avoidance and recovery (timeouts, rollback and retry, thread dumps).
- Explain the event loop and the rule never to block it; say where CPU work goes.
- Explain the GIL honestly: it makes some races rarer but does not remove them, and it does not stop
multiprocessingor I/O waiting from being concurrent. - Say how you would find a bug: a race detector (
go -race, ThreadSanitizer), stress tests that repeat many times with many threads, thread dumps for hangs.
Questions interviewers like to add:
- "Why does a mutex make a program slower, and when is that fine?" Tasks wait for each other; fine when the critical section is short and contention is low.
- "Is a
volatileor atomic flag enough to protect a data structure?" No. It protects one value; multi-step updates need a lock or a single owner. - "One thread, so no races?" Only if no
awaitor callback can run between your check and your act. See section 2. - "How many threads should a pool have?" About the core count for CPU-bound work, more for work that waits; measure it.
- "Your service is hung and CPU is idle. What do you do?" Take thread dumps, look for threads waiting on each other, check pool sizes and downstream timeouts.
Quick recap
- Concurrency is overlapping tasks; parallelism is running them on several cores.
- Threads share memory, event loops run one callback at a time, goroutines can share memory or pass messages over channels, and actors only pass messages.
- A data race is unsynchronised access to shared memory; a race condition is any timing-dependent bug, and it can exist without a data race.
counter += 1is read, add, write, and two of them can lose an update (30 to 44 percent lost in the Go experiment). - A passing test proves little: the Python GIL hid the lost update in 12 million increments and a single
sleep(0)exposed it at once. - Fix races with a lock, an atomic, a single owner with messages, or immutable data, and keep critical sections small.
- A deadlock needs mutual exclusion, hold and wait, no preemption and circular wait; a global lock order prevents the circle, and timeouts or rollback recover when it happens anyway.
- Never block an event loop: a 500 ms busy loop made a 100 ms timer fire at 504 ms.
- Bound the concurrency, pass cancellation, and look at the results of background tasks.
Appendix: the experiment code
Every number above came from one of these programs. Run them yourself; expect different numbers on a different machine, and different numbers on each run.
Go: lost update, mutex, atomic and channel
Run with go run . (counts and times) and go run -race . (the race detector report). Save it as main.go in a folder with a go.mod (go mod init race).
package main
import (
"fmt"
"sync"
"sync/atomic"
"time"
)
const (
workers = 4
perWork = 1_000_000
)
func run(name string, work func(wg *sync.WaitGroup)) {
var wg sync.WaitGroup
start := time.Now()
for i := 0; i < workers; i++ {
wg.Add(1)
go work(&wg)
}
wg.Wait()
fmt.Printf("%-8s took %v\n", name, time.Since(start).Round(time.Millisecond))
}
func main() {
want := workers * perWork
for trial := 0; trial < 3; trial++ {
plain := 0
run("plain", func(wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < perWork; i++ {
plain++
}
})
fmt.Printf(" expected %d got %d lost %d\n", want, plain, want-plain)
}
var mu sync.Mutex
locked := 0
run("mutex", func(wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < perWork; i++ {
mu.Lock()
locked++
mu.Unlock()
}
})
fmt.Printf(" expected %d got %d\n", want, locked)
var atom atomic.Int64
run("atomic", func(wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < perWork; i++ {
atom.Add(1)
}
})
fmt.Printf(" expected %d got %d\n", want, atom.Load())
// one goroutine owns the counter; the others send it messages
ch := make(chan struct{}, 1024)
done := make(chan int)
go func() {
n := 0
for range ch {
n++
}
done <- n
}()
run("channel", func(wg *sync.WaitGroup) {
defer wg.Done()
for i := 0; i < perWork; i++ {
ch <- struct{}{}
}
})
close(ch)
fmt.Printf(" expected %d got %d\n", want, <-done)
}
Python: the GIL and the lost update
Run python3 race.py. The first part is the plain counter += 1 loop; set explicit to True to use the read, pause, write version (it is slow: about a minute with the lock, so lower N if you are impatient).
import sys, threading, time
explicit = False
N = 200_000 if explicit else 1_000_000
counter = 0
lock = threading.Lock()
def bump(use_lock):
global counter
for _ in range(N):
if use_lock:
lock.acquire()
if explicit:
value = counter # read
time.sleep(0) # lets another thread run
counter = value + 1 # write back
else:
counter += 1
if use_lock:
lock.release()
def run(use_lock, threads=4):
global counter
counter = 0
ts = [threading.Thread(target=bump, args=(use_lock,)) for _ in range(threads)]
start = time.perf_counter()
for t in ts: t.start()
for t in ts: t.join()
return counter, time.perf_counter() - start
print(sys.version.split()[0], "GIL enabled:", getattr(sys, "_is_gil_enabled", lambda: True)())
for use_lock in (False, True):
for _ in range(3):
got, secs = run(use_lock)
print(f"lock={use_lock} expected {4 * N:,} got {got:,} in {secs:.2f}s")
JavaScript: a check-then-act race and a blocked event loop
Run each file with node (Node 22 or later; the files are ES modules, so name them .mjs).
// race.mjs
const audit = () => new Promise((resolve) => setTimeout(resolve, 10));
let balance = 100;
async function withdraw(amount) {
if (balance >= amount) {
await audit();
balance -= amount;
}
}
await Promise.all([withdraw(80), withdraw(80)]);
console.log("unprotected:", balance); // -60
And the blocked event loop:
// blocked.mjs: a timer asked for 100 ms behind 500 ms of busy work
const start = performance.now();
setTimeout(() => console.log(`timer ran after ${Math.round(performance.now() - start)} ms`), 100);
const until = performance.now() + 500;
while (performance.now() < until) { /* busy */ }
console.log(`busy loop finished at ${Math.round(performance.now() - start)} ms`);
// replace the busy loop with: await new Promise((r) => setTimeout(r, 500));
// and the timer runs after about 100 ms
Python asyncio: blocking the loop
import asyncio, time
async def ticker(start):
for _ in range(3):
await asyncio.sleep(0.1)
print(f" tick at {time.perf_counter() - start:.2f}s")
async def blocking(start):
time.sleep(0.5) # blocks the whole event loop
async def friendly(start):
await asyncio.sleep(0.5) # yields while waiting
async def main(work):
start = time.perf_counter()
await asyncio.gather(ticker(start), work(start))
asyncio.run(main(blocking))
asyncio.run(main(friendly))
Go: the cost of a parked goroutine
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
func main() {
const n = 100_000
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
start := time.Now()
stop := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < n; i++ {
wg.Add(1)
go func() { defer wg.Done(); <-stop }()
}
time.Sleep(200 * time.Millisecond)
runtime.ReadMemStats(&after)
fmt.Printf("%d goroutines parked: %.1f KiB each (stacks), created in under %v\n",
n, float64(after.StackInuse-before.StackInuse)/1024/n, time.Since(start).Round(10*time.Millisecond))
close(stop)
wg.Wait()
}
Sources
- The Go Project, The Go Memory Model. When a write by one goroutine is guaranteed to be seen by another, and why racy programs are unsafe.
- Go documentation, Data Race Detector. The
-raceflag used in section 2. - Python documentation, threading and asyncio.
- Sam Gross, PEP 703: Making the Global Interpreter Lock Optional in CPython.
- Node.js documentation, The Node.js Event Loop.
- E. G. Coffman, M. J. Elphick and A. Shoshani, "System Deadlocks", ACM Computing Surveys 3(2), 1971. The four conditions in section 4.
- Microsoft, Managed threading best practices. Locks,
Interlockedand deadlock guidance for .NET.
Practise this topic: CS Fundamentals