Concurrency and multithreading: threads, locks, races and async I/O – Server Side interview prep

261 interview-style multiple-choice questions on Concurrency and multithreading: threads, locks, races and async I/O, organized by topic: concepts, scenarios, code to read and tradeoffs, each with a short explanation. Learn the 69 key terms with the flashcards first, then test yourself.

Take the practice test · Flashcards · All study tools

What it covers

  1. 12.1 Processes, threads and scheduling
  2. 12.2 Race conditions, visibility and memory models
  3. 12.3 Locks and synchronization primitives
  4. 12.4 Atomics, lock-free code and contention
  5. 12.5 Deadlock, livelock and starvation
  6. 12.6 Thread pools, tasks, futures and structured concurrency
  7. 12.7 Async I/O, event loops and coroutines
  8. 12.8 Message passing: queues, channels and actors
  9. 12.9 Concurrency patterns, distributed coordination and testing

Key terms

12.1 Processes, threads and scheduling

Concurrency
Making progress on several tasks by interleaving
Parallelism
Running tasks simultaneously on multiple cores
Thread pool
Reusable worker threads for queued tasks
Amdahl's law
Serial parts limit parallel speedup
Preemption
The OS interrupting a running thread
Green thread
Runtime-scheduled lightweight thread
Gustafson's law
Bigger problems scale with more cores
Little's law
L = λW
Copy-on-write
Share pages until written
NUMA
Memory speed depends on socket

12.2 Race conditions, visibility and memory models

Race condition
Timing-dependent bug on shared state
Mutex
Lock allowing one thread in a critical section
Atomic operation
Indivisible operation on shared memory
Semaphore
Counter limiting concurrent access
Data race
Unsynchronized access with at least one write
Check-then-act
Acting on a check that may be stale
Happens-before
Ordering that guarantees visibility
Memory barrier
Stops reordering across a point
Safe publication
Sharing an object fully built
Thread confinement
Data used by only one thread

12.3 Locks and synchronization primitives

Critical section
Code that must not run concurrently
Reentrant lock
Owner can reacquire it
Spinlock
Busy-waits for a lock
Condition variable
Wait until state changes
Barrier
All threads wait at a point
Lock striping
Separate locks per segment

12.4 Atomics, lock-free code and contention

Compare-and-swap
Conditional atomic update
ABA problem
Value changes and returns, fooling CAS
Lock-free
Some thread always progresses
Wait-free
Every thread progresses in bounded steps
False sharing
Separate variables on one cache line
RCU
Lock-free reads, versioned updates

12.5 Deadlock, livelock and starvation

Deadlock
Threads waiting on each other forever
Livelock
Threads busy reacting but making no progress
Starvation
A thread never getting needed resources
Lock ordering
Acquiring locks in a fixed global order
Dining philosophers
Classic deadlock and starvation example
Wait-for graph
Who waits on whom
Banker's algorithm
Deadlock avoidance by safe states
Priority inheritance
Lock holder borrows waiter's priority
Aging
Raising priority while waiting
Thread dump
Snapshot of all threads and locks

12.6 Thread pools, tasks, futures and structured concurrency

Future
Placeholder for a later result
Work stealing
Idle workers take others' queued tasks
Rejection policy
What a full pool does with new tasks
Structured concurrency
Child tasks bound to a scope
Cooperative cancellation
Tasks check for cancel requests
Thread pool starvation
All threads blocked, work stuck

12.7 Async I/O, event loops and coroutines

Non-blocking I/O
Calls return immediately if not ready
epoll
Linux readiness notification
io_uring
Linux completion-based I/O rings
Reactor pattern
React to readiness, then do I/O
Coroutine
Function that suspends and resumes
Function coloring
Async spreading through call chains

12.8 Message passing: queues, channels and actors

Producer-consumer
Queue between work producers and consumers
Actor model
Message-passing actors with private state
Optimistic concurrency
Check for conflicts at commit time
Fan-out/fan-in
Spread work out, then gather results
Blocking queue
Queue whose put/take wait
SPSC queue
Single producer, single consumer
CSP
Processes communicating over channels
Supervision tree
Supervisors restart failed children
Virtual actor
Actor activated on demand
Poison pill
Message telling a consumer to stop

12.9 Concurrency patterns, distributed coordination and testing

Fencing token
Monotonic number rejecting stale lock holders
Lease
Time-limited, renewable lock
Deterministic simulation
Replayable testing with simulated I/O and time
TLA+
Specification language for concurrent designs
Single-writer principle
One owner for all writes

Sample practice questions

  1. What is the difference between concurrency and parallelism?

    1. They mean exactly the same thing and are used interchangeably by engineers
    2. Concurrency only applies to databases; parallelism only applies to CPUs
    3. Parallelism interleaves many tasks; concurrency runs them at once on several cores
    4. Concurrency interleaves many tasks; parallelism runs them at once on several cores

    Answer: D. Concurrency interleaves many tasks; parallelism runs them at once on several cores A single-core machine can be concurrent but not parallel.

  2. What is a race condition?

    1. A loop whose exit condition is never reached
    2. A benchmark competition between two programs for speed
    3. A bug where the result depends on unpredictable thread timing
    4. One thread consistently running faster than another

    Answer: C. A bug where the result depends on unpredictable thread timing Races often only show up under load.

  3. What does a mutex (lock) guarantee?

    1. Threads run strictly in the order they were created
    2. Only one thread at a time runs the critical section
    3. The protected code always runs faster than before
    4. No thread ever has to wait to enter the section

    Answer: B. Only one thread at a time runs the critical section Hold locks for as short a time as possible.

  4. What does an atomic operation provide?

    1. Encryption of the value while it's in memory
    2. An indivisible read-modify-write, like compare-and-swap
    3. A guarantee that the code only runs on one CPU core
    4. Automatic retries whenever the operation fails

    Answer: B. An indivisible read-modify-write, like compare-and-swap Atomics are cheaper than locks for simple counters and flags.

  5. What are the four Coffman conditions that must all hold for a deadlock?

    1. Shared memory, busy waiting, priority boost, linear wait
    2. Mutual exclusion, hold and wait, no preemption, circular wait
    3. Mutual exclusion, starvation, preemption, lock ordering
    4. Read, write, lock and unlock all happening concurrently

    Answer: B. Mutual exclusion, hold and wait, no preemption, circular wait Breaking any one of them prevents deadlock.

  6. What is a thread pool?

    1. Reusable worker threads that run queued tasks
    2. A pool of open connections to a database
    3. A group of processes spread across machines
    4. A list of finished threads waiting to be deleted

    Answer: A. Reusable worker threads that run queued tasks Pools also cap concurrency so the system isn't overwhelmed.

  7. Which kind of work benefits most from async, non-blocking I/O?

    1. Work that never has to wait on anything
    2. I/O-bound work, like many network or database calls
    3. CPU-bound work, like compressing large images
    4. A single short calculation done in memory

    Answer: B. I/O-bound work, like many network or database calls For CPU-bound work, use parallel threads or processes instead.

  8. What is the main benefit of an immutable object in concurrent code?

    1. Threads can share it safely without locks
    2. It runs faster when only one thread uses it
    3. It always uses less memory than a mutable one
    4. It can never be garbage collected by the runtime

    Answer: A. Threads can share it safely without locks Prefer immutability and message passing over shared mutable state.

  9. What is a common way to make a shared counter in a web app correct across several server instances?

    1. An atomic op in a shared store, like Redis `INCR`
    2. A static variable in the server's application code
    3. A local mutex inside each server instance's process
    4. Incrementing the counter in each user's browser

    Answer: A. An atomic op in a shared store, like Redis `INCR` In-process locks don't coordinate across machines.

  10. What is the difference between a process and a thread?

    1. They're identical in all modern operating systems today
    2. A thread can contain many processes, each with its own memory
    3. Processes have separate memory; a process's threads share it
    4. Threads have separate memory; processes share one memory space

    Answer: C. Processes have separate memory; a process's threads share it Shared memory makes threads cheap to communicate but prone to races.

Practice all 261 questions

More Server Side