Barriers and Condition Variables Fundamentals Quiz Quiz

Assess your understanding of key concepts in barriers and condition variables, including synchronization, thread coordination, and simple practical examples. This quiz helps reinforce essential knowledge for working with concurrent programming constructs.

  1. Purpose of a Barrier

    What is the main purpose of a barrier in concurrent programming when multiple threads reach it during execution?

    1. To end a thread's execution when it reaches the barrier
    2. To permanently stop threads that reach the barrier first
    3. To delete all local data of threads at the barrier
    4. To pause all threads until every participating thread arrives at the barrier

    Explanation: A barrier synchronizes threads by forcing them to wait until all have reached a specific point before any can proceed. It does not stop threads permanently or end executions; it only pauses them until the condition is met. Deleting local thread data at a barrier is not a function of the barrier. The correct answer clearly describes the collective wait imposed by a barrier.

  2. Condition Variable Usage

    In a producer-consumer scenario, why would a consumer thread use a condition variable before accessing a shared buffer?

    1. To decrease the number of producers
    2. To remove the buffer from memory
    3. To wait for the buffer to contain items before continuing
    4. To copy all items into its local storage before proceeding

    Explanation: A consumer thread uses a condition variable to sleep and wait until items are available, ensuring safe access to the buffer. Deleting the buffer or decreasing the number of producers are incorrect uses of condition variables. Copying all items before proceeding is not their function. Only waiting for a signal to continue describes their intended use.

  3. Distinct Feature of Barriers

    Which feature distinguishes barriers from condition variables in thread synchronization?

    1. Barriers require all participating threads to arrive before any can proceed
    2. Condition variables terminate threads on notification
    3. Barriers allow a single thread to continue independently
    4. Condition variables automatically lock resources

    Explanation: Barriers enforce that all involved threads must wait until each has reached a certain point, after which they may all proceed. Condition variables do not terminate threads and do not automatically acquire locks. Allowing a single thread to continue is not a property of barriers; rather, they facilitate group progress.

  4. Signaling in Condition Variables

    What happens when a thread signals a condition variable using a notify operation?

    1. All waiting threads are stopped permanently
    2. Locks are released automatically for all threads
    3. Every thread in the process restarts from the beginning
    4. At least one waiting thread is awakened to proceed

    Explanation: Signaling a condition variable wakes at least one thread that is waiting, allowing it to check whether it can continue. Not all waiting threads are stopped; locks are not universally released. The process does not restart threads from the beginning. Only the correct answer matches the effect of a notify operation.

  5. Typical Scenario for Barriers

    In which situation is a barrier most appropriately used among threads?

    1. When a thread needs exclusive access to a resource
    2. When data must be passed from one thread to another
    3. When all threads must finish a computation phase before moving to the next phase
    4. When a thread must acquire a lock repeatedly

    Explanation: Barriers synchronize threads so that no thread can move to the next stage until all have completed the current one. Exclusive access to resources or lock acquisition is handled by mutexes or locks, not barriers. Passing data is not the barrier’s main function. The selected answer captures the purpose of a barrier.

  6. Common Use of Condition Variables

    Which of these is a common use case for condition variables in a concurrent application?

    1. To terminate non-responsive threads silently
    2. To manage memory leaks across threads
    3. To allocate shared memory between threads
    4. To allow threads to wait for a specific condition to become true

    Explanation: Condition variables enable one or more threads to sleep until a specific condition is met, such as availability of data. They do not terminate threads, manage memory leaks, or allocate shared memory directly. Only waiting for a condition matches their intended role.

  7. Effect of Missing notify in Condition Variables

    If a notify signal is never sent to a condition variable, what effect does it have on threads that are waiting on it?

    1. All threads immediately exit with an error
    2. Waiting threads may block indefinitely and not proceed
    3. Threads become immune to future locks
    4. Threads will eventually proceed without a signal

    Explanation: Without a notify, threads waiting on a condition variable can be blocked forever, as they rely on the signal to wake up. Threads do not proceed on their own, nor do they exit automatically. There is no concept of immunity to locks; only indefinite blocking matches the scenario.

  8. Creating a Barrier in Practice

    Suppose you want to synchronize the start of 4 threads so that none of them start their main work until all are ready. Which synchronization primitive should you use?

    1. Barrier
    2. Buffer
    3. Signal
    4. Message queue

    Explanation: A barrier is specifically designed to make threads wait until a set number have reached the barrier, allowing coordinated starts. Signals can notify but do not synchronize groups. Buffers store data, not coordinate execution. Message queues pass messages but do not relate to mutual synchronization as required here.

  9. Role of Mutex with Condition Variable

    Why is a mutex commonly used together with a condition variable when threads wait for a condition?

    1. To permanently lock resources from other threads
    2. To protect shared data during checks and ensure consistent state
    3. To increase the condition variable's memory capacity
    4. To make threads run twice as fast

    Explanation: A mutex ensures that shared data are not modified by multiple threads at once, avoiding race conditions, especially when checking and waiting for conditions. Mutexes are not for permanently locking resources. They do not speed up threads or alter memory size for condition variables. The correct answer describes their synchronization purpose.

  10. Barrier Behavior Example

    If five threads reach a barrier and one is delayed, what will the other threads do until the last thread arrives?

    1. Only the slowest thread will proceed
    2. Threads will skip their computation and terminate
    3. The waiting threads will leave the barrier immediately
    4. The first four threads will wait at the barrier until the fifth arrives

    Explanation: Barriers force all threads to wait until all participants have reached the synchronization point, so early threads must pause. The threads do not leave the barrier until all have arrived, nor do they terminate their tasks. The slowest thread does not proceed alone; all threads pass together once everyone has arrived.