Which of the below given is/are correct for implementing thread-safety in a multi-threaded...
Micron technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Which of the below given is/are correct for implementing thread-safety in a multi-threaded program?
I) Allocating separate private space to every thread in the program so that all threads safely read/write shared data at a time.
II) Allowing not more than one thread to read/write shared data.
III) Using atomic actions with shared data.
Show answer & explanation
All three statements represent valid thread-safety mechanisms. (I) describes thread-local storage, which eliminates contention by giving each thread private data. (II) describes mutual exclusion/locks, which serializes access to shared resources. (III) describes atomic operations, which guarantee indivisible read-modify-write sequences without explicit locks. Together, these represent the major categories of thread-safety implementation strategies.
Step-by-step Derivation:
Analyzing each statement:
(I) Thread-local storage: By allocating separate private space (thread-local variables) to each thread, threads never contend over that data. This is a valid thread-safety technique (e.g., Java's ThreadLocal, C++ thread_local). ✓
(II) Mutual exclusion: Restricting shared data access to one thread at a time (via locks, mutexes, synchronized blocks) prevents race conditions and data corruption. This is the foundational thread-safety mechanism. ✓
(III) Atomic operations: Atomic actions ensure that read-modify-write operations on shared data complete indivisibly without interleaving from other threads. This is valid for simple operations (e.g., compare-and-swap, atomic integers). ✓
All three are correct approaches to achieving thread-safety, though they may be applied in different contexts. The answer is A.