Questions: Thread Scheduling and Coordination

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

Two threads both execute `counter += 1`. Thread A loads counter=5 and is preempted. Thread B loads 5, increments to 6, and stores. Thread A resumes and stores 6. The final value is 6, not 7. This is best described as:

AA scheduler bug — the OS should never preempt a thread mid-operation
BA hardware bug — CPUs should make read-modify-write atomic by default
CA race condition — the program accesses shared data without synchronization
DA memory allocation bug — both threads should have their own copy of counter
Question 2 Multiple Choice

After adding a mutex lock around `counter += 1`, a team notices that 8 threads incrementing the counter runs far slower than expected. The most likely explanation is:

AThe mutex is broken — threads are still racing despite the lock
BMutexes have O(n) acquisition cost where n is the number of waiting threads
CLock contention — threads spend significant time blocked waiting for the lock rather than doing useful work
DThe compiler has eliminated the mutex as dead code during optimization
Question 3 True / False

A race condition can primarily occur when two threads run simultaneously on multiple CPU cores; single-core systems are immune.

TTrue
FFalse
Question 4 True / False

Two threads that access shared memory but never write to it simultaneously can safely do so without any synchronization.

TTrue
FFalse
Question 5 Short Answer

Explain why `counter += 1` is not an atomic operation even though it appears as a single statement in source code. What must a programmer do to ensure this operation is safe when shared between threads?

Think about your answer, then reveal below.