Wk4L2 - Race Conditions and Barriers Flashcards
What is a Race Condition?
A race condition occurs when the timing or order of thread execution affects the correctness of the result, even when mutual exclusion is used to prevent concurrent writes to shared resources.
How is a Race Condition different from a Data Race?
A data race happens when multiple threads write to the same memory location without proper synchronization, potentially causing data loss. A race condition, however, arises from the order or timing of execution affecting the outcome, even if mutual exclusion is in place.
What is a real-world analogy for a Race Condition?
Imagine agreeing to meet a friend at Tim Hortons on campus, but there are multiple locations. The outcome depends on the order you check the locations and how long you wait. Similarly, in programming, race conditions depend on the execution order of threads.
What is a Barrier in multithreading?
A barrier is a synchronization point where multiple threads stop and wait for each other before continuing execution. Unlike a join, threads do not terminate; they just pause until all threads have reached the barrier.
What is a CyclicBarrier in Java?
A CyclicBarrier is a reusable barrier that waits for a set number of threads (called parties) to reach the barrier before allowing them to proceed. After the barrier is crossed, it can be reused for the next step.
When are Barriers typically used?
Barriers are often used in simulations where threads complete steps in parallel. Each thread finishes a step, waits at the barrier for other threads to complete the same step, and then moves on to the next step.