Which memory synchronization primitive is used to protect a critical section so that only one thread can enter at a time?

Enhance your understanding with the System Software, Architecture, Memory and Storage Test. Study with flashcards and multiple choice questions. Each question offers hints and detailed explanations. Prepare effectively for your exam!

Multiple Choice

Which memory synchronization primitive is used to protect a critical section so that only one thread can enter at a time?

Explanation:
Mutual exclusion is the goal: ensure that only one thread can execute a critical section at a time. A mutex provides this by requiring a thread to acquire the lock before entering the protected region; if another thread tries to enter while it's held, it blocks until the lock is released. This blocking behavior saves CPU time and establishes a clear ownership and memory visibility order: changes inside the critical section become visible after unlocking. While condition variables are used with a mutex to wait for conditions, they don’t by themselves protect a section of code. Semaphores control access with a count and can allow multiple concurrent entries unless the count is 1, and spinlocks busy-wait, which can waste CPU time for longer waits. For protecting a critical section so only one thread enters at a time in typical scenarios, a mutex is the appropriate primitive.

Mutual exclusion is the goal: ensure that only one thread can execute a critical section at a time. A mutex provides this by requiring a thread to acquire the lock before entering the protected region; if another thread tries to enter while it's held, it blocks until the lock is released. This blocking behavior saves CPU time and establishes a clear ownership and memory visibility order: changes inside the critical section become visible after unlocking. While condition variables are used with a mutex to wait for conditions, they don’t by themselves protect a section of code. Semaphores control access with a count and can allow multiple concurrent entries unless the count is 1, and spinlocks busy-wait, which can waste CPU time for longer waits. For protecting a critical section so only one thread enters at a time in typical scenarios, a mutex is the appropriate primitive.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy