Multicore & Synchronization
Learn about the MESI protocol, memory consistency models, atomic instructions such as CAS and LL/SC, locks and memory barriers, false sharing and data races with multiple-choice questions.
Multicore & Synchronization: key points
In a multicore CPU, several cores on one chip share the same memory while each keeps its own caches. Coherence protocols such as MESI manage cache-line states so that no core keeps using stale data after another core writes it.
Beyond that, you need to understand memory consistency models, which define the order in which writes become visible to other cores, atomic instructions that let cores update values safely, and locks and memory barriers. This topic also covers pitfalls such as false sharing and data races, which make programs that look correct run slowly or misbehave.
All Multicore & Synchronization questions (50)
- Two cores each hold a copy of variable x in their own caches. After core 0 writes x, what problem could occur when core 1 reads x if there were no cache coherence mechanism?
- Which correctly lists the four states of the MESI cache coherence protocol?
- In the MESI protocol, what does it mean when a cache line is in the Modified (M) state?
- In the MESI protocol, what happens when a core writes to a line it holds in the Exclusive (E) state?
- In an invalidation-based coherence protocol, what happens when a core writes to a line in the Shared (S) state?
- In MESI, when core 1 tries to read a line that core 0 holds in the Modified (M) state, what generally happens?
- Which describes the snooping approach to cache coherence?
- Why do systems with many cores use directory-based coherence instead of snooping?
- Coherence protocols can be invalidation-based or update-based. Why do most CPUs use invalidation?
- Which describes false sharing in a multithreaded program?
- False sharing occurs with an array of per-thread counters such as "long cnt[8];". What is the most suitable fix?
- Which describes a data race?
- When two threads run the code above simultaneously, the final counter can be less than 200000. Why?
- Which describes an atomic instruction?
- What does the atomic CAS (compare-and-swap) instruction do?
- The code above doubles the shared variable counter using CAS(&p, expected, new). When does the CAS fail so the loop repeats?
- With an instruction pair such as RISC-V LR/SC (load-reserved / store-conditional), when does the SC fail?
- Which describes the ABA problem that affects lock-free data structures built with CAS?
- What does a fetch-and-add instruction, such as RISC-V amoadd.w or x86 lock xadd, do?
- Which describes a spinlock?
- In which situation is a spinlock least suitable?
- Why does a spinlock usually wait with ordinary reads and use an atomic instruction only when the lock looks free (test-and-test-and-set), instead of retrying the atomic write directly?
- Which correctly compares a mutex (a lock that sleeps while waiting) with a spinlock?
- Which is an example of deadlock?
- In a program using several locks, what is the most common way to prevent deadlock?
- What does a memory consistency model define?
- Which describes sequential consistency?
- The two threads above run simultaneously on different cores (assume no compiler reordering). Which outcome cannot occur under sequential consistency but can occur on real CPUs such as x86?
- What is a characteristic of the x86 memory consistency model (TSO, total store order)?
- Which statement is correct about weak memory consistency models such as those of Arm and RISC-V (RVWMO)?
- What is the role of a memory barrier (fence) instruction?
- In the code above, thread 1 prepares data and then signals with flag, and thread 2 waits for flag and then reads data. On a CPU with a weak memory model, what is needed for thread 2 to reliably read 42?
- Which statement is correct about memory operations with acquire and release semantics?
- In C, why is marking a flag variable shared between threads volatile not sufficient for synchronization?
- Why do C11 _Atomic and C++11 std::atomic let programmers specify a memory order?
- Which correctly compares coarse-grained locking (one lock for a whole large data structure) with fine-grained locking (separate locks for each part)?
- What property does a lock-free algorithm have?
- Which describes a critical section?
- Which describes barrier synchronization in parallel programs?
- When 64 cores continuously update one shared counter with atomic instructions, adding cores does not speed things up. What is the main reason?
- Which describes a NUMA (non-uniform memory access) system?
- Under the first-touch policy common on Linux NUMA systems (a page is placed in the memory of the node where the thread first writing it runs), what should programmers watch out for?
- Which is NOT a reason a parallel program fails to scale as cores are added?
- When splitting loop iterations among threads, which division suits iterations whose costs vary widely?
- When several threads sum all elements of an array, which approach performs well?
- What is a characteristic of a reader-writer lock?
- What is the most suitable use of a counting semaphore?
- What is the difference between cache coherence and a memory consistency model?
- Which describes the idea of hardware transactional memory?
- When waiting on a condition variable until a queue becomes non-empty, why is the condition rechecked in a while loop rather than an if after waking?