Try to answer before you open the answers. Guessing is fine; checking your guess is how you learn.


QUICK REVIEW (5 minutes)

  1. What is a thread, in one sentence?
  2. Name two things threads of the same process share and two things each thread has of its own.
  3. What does Windows schedule: processes or threads?
  4. What is a quantum?
  5. What does preemptive multitasking mean?
  6. Name the three main states a thread moves between, and the final one.
  7. What range of priorities is "real-time" in Windows, and what range is "dynamic"?
  8. What gets saved during a context switch?
  9. Which lock works only inside one process and is faster than a mutex?
  10. Why do programs use a thread pool instead of creating a thread for every small job?

ANSWER KEY — DO NOT READ UNTIL YOU'VE ANSWERED

Click to reveal answers

1. Thread

The unit of work the OS gives to a CPU core: one path of instructions running inside a process.

2. Shared vs own

Shared: code, heap, global variables, handles, address space (any two). Own: stack, registers, thread ID, priority and state (any two).

3. What is scheduled

Threads. A process is just the owner of memory and handles.

4. Quantum

The time slice: how long a thread may run before the scheduler can swap it for an equal-priority thread (about 20 to 30 ms on Windows client).

5. Preemptive

The OS can take the CPU away from a thread by force (using a timer interrupt, or when a more important thread is ready), without the thread's agreement.

6. States

Ready, Running and Waiting, then Terminated at the end. (Windows also has Initialized, Standby and Transition.)

7. Priority ranges

Real-time is 16 to 31. Dynamic is 1 to 15. (Priority 0 is reserved for a special system thread.)

8. Context switch

The CPU state: instruction pointer, stack pointer, general registers, flags and (when used) floating-point/vector registers. Memory contents are not copied.

9. Lightweight lock

The critical section (and the even smaller SRW lock).

10. Thread pool

Creating and destroying threads costs time and memory, and too many threads cause heavy context switching. A pool reuses a limited number of threads and queues the extra tasks.


UNDERSTANDING QUESTIONS

Q1. Why is a context switch between two threads of the same process cheaper than between threads of two different processes?

Answer Within one process, the address space stays the same. Only registers and the stack pointer change. Across processes, Windows must also switch the page-table pointer, and the TLB (translation cache) holds translations for the wrong process, so more extra work and slower memory accesses follow.

Q2. Why does a "race condition" sometimes appear only on a faster PC or when many cores are used?

Answer A race depends on the exact timing of two threads touching the same data. On a slower or single-core PC, one thread may usually finish its small step before the other starts, so the bad overlap is rare. With more cores, threads truly run at the same instant, so the bad overlap is much more likely.

Q3. A deadlocked program shows 0% CPU, but a livelocked one may show high CPU. Why?

Answer Deadlocked threads are all in the Waiting state: asleep, using no CPU. Livelocked threads are still Running or Ready: they keep retrying and reacting to each other, so they burn CPU while making no progress.

Q4. Why does Windows boost the priority of a thread that has just finished waiting for keyboard input?

Answer So that the thread reacts quickly. It spent a long time asleep and uses little CPU, so a short boost lets it jump ahead of busy background threads. The boost fades by one level for each full quantum used, so it cannot starve others.

Q5. Why is it a bad idea to run long, slow tasks (such as network waits) on the UI thread?

Answer The UI thread must keep reading window messages. If it is stuck on a slow job, it cannot repaint or answer clicks, and Windows marks the window "Not Responding". Slow work belongs on a worker thread.

SCENARIO QUESTIONS (Troubleshooting)

S1. A program freezes. Task Manager shows 0% CPU for it and "Not Responding". What do you suspect, and which tool can help?

Answer A deadlock (or a thread waiting forever on something that never happens). Use Resource Monitor → CPU tab → right-click the program → Analyze Wait Chain, or look at thread stacks in Process Explorer or a debugger.

S2. A video render makes your whole PC feel slow, though it finishes fine. How can you keep the PC lively without stopping the render?

Answer Lower the render program's priority to Below Normal or Idle (Task Manager → Details → Set priority, or start it with a lower class). Other programs then get cores first, and the render uses what is left.

S3. A server creates a new thread for each of 5,000 incoming requests. Under load, it becomes very slow and uses lots of memory. What is the problem and the fix?

Answer Too many threads: each uses a stack and kernel memory, and thousands of threads fighting for a few cores cause heavy context switching. Use a thread pool (with a limited number of threads and a queue), or asynchronous I/O.

S4. Two threads each add 1,000,000 to a shared counter. The final total is less than 2,000,000, and different each run. Why, and how do you fix it?

Answer A race condition: the add is really load, add, store, and updates are lost when the threads interleave. Fix it with `InterlockedAdd`/`InterlockedIncrement`, or protect the counter with a lock, or give each thread its own counter and add them at the end.

S5. A low-priority logging thread holds a lock. A high-priority UI thread needs it, but a medium-priority thread keeps the CPU busy. The UI stutters for seconds. What is this, and how is it improved?

Answer Priority inversion. Improve it by keeping the lock very short, not sharing that lock between high- and low-priority threads, or using a design with message queues. Windows eventually boosts the starved low-priority thread (the balance-set manager), but that takes seconds.

INTERVIEW QUESTIONS

I1. Explain the difference between a process and a thread.

Answer A process is a container that owns an address space, handles and security context. A thread is an execution path inside it, with its own stack, registers and ID, sharing the process's memory. The OS schedules threads. A process has at least one thread, and threads are cheaper to create and switch, but a fault in one can bring down all of them.

I2. Describe the life of a thread through the Windows thread states.

Answer Initialized when created, then Ready in a ready queue. The scheduler chooses it for a core (Standby) and it becomes Running. It leaves Running when its quantum ends (back to Ready), when it must wait (Waiting; later Ready again, via Transition if its kernel stack was paged out), or when it ends (Terminated).

I3. What are the four conditions for a deadlock, and how would you prevent one?

Answer Mutual exclusion, hold and wait, no preemption, and circular wait. Prevent it by breaking one: most commonly, acquire locks in a fixed global order (no circular wait), or use try-lock with a timeout and release what you hold, or avoid holding more than one lock.

I4. When would you choose a critical section, a mutex, a semaphore, an event, or an SRW lock?

Answer Critical section: ordinary lock inside one process. Mutex: needed across processes (named) or when you want abandoned-owner detection. Semaphore: limit N concurrent users or count items. Event: signal that something happened. SRW lock: read-mostly data with many readers and rare writers. For plain counters, use atomic Interlocked operations.

I5. Why do thread pools improve performance, and what can still go wrong with them?

Answer They avoid the cost of creating and destroying threads and cap the number of threads, which reduces memory and context switching. Problems remain: blocking tasks can use up all pool threads (starvation or pool deadlock when tasks wait for other queued tasks), and shared data still needs synchronization.

PRACTICAL TASK

Build a one-page "thread report" for a program you use every day. Using the Practical Lab, pick one program (for example, your browser or File Explorer). Record:

  1. Its thread count and handle count (Task Manager → Details).
  2. The states of its first 15 threads ((Get-Process -Name <name>).Threads).
  3. How many threads look like thread pool workers (Process Explorer start address TppWorkerThread).
  4. Context Switches/sec and Processor Queue Length for the whole PC, once while the program is idle and once while it is busy.

Then write 3 sentences: is the program mostly waiting or computing? Does it look like it uses a pool? Is the PC's CPU overloaded when it is busy?

Why This Matters

When a program is slow, frozen or leaking memory, threads are often the reason. If you can see how many exist, what they wait for and how busy the scheduler is, you can tell the difference between "waiting on disk", "deadlocked", "overloaded" and "leaking" very quickly.


CHAPTER SUMMARY

  • A thread is the unit of CPU scheduling inside a process. Threads share the process's heap, code and handles, but each has its own stack, registers and ID.
  • The main thread starts with the process; worker threads are created with CreateThread and similar calls. Never kill a thread forcibly (TerminateThread) unless you must.
  • The scheduler runs the highest-priority ready thread, gives equal threads time slices (quanta), and preempts using timer interrupts. It tries to keep a thread on the same core (soft affinity) and can be limited with an affinity mask.
  • Thread states: Initialized, Ready, Standby, Running, Waiting, Transition, Terminated.
  • Priority runs 0 to 31: dynamic 1–15 (with boosts) and real-time 16–31. Priority inversion is a high thread blocked by a low one while a medium one runs.
  • A context switch saves and restores registers and the stack pointer. Direct cost is a few microseconds; hidden costs include cold caches and TLB effects.
  • Synchronization tools: atomic Interlocked operations, critical sections, SRW locks, mutexes, semaphores, events and condition variables.
  • Concurrency problems: race conditions, deadlock, livelock, starvation. Prevent them with less sharing, short locks, a fixed lock order and queues.
  • Thread pools reuse a limited number of threads to run many small tasks. Windows has one built in.