Question

You are designing a system where multiple threads need to access and modify a shared counter variable. What is a critical concern you must address to ensure correctness?

A Memory leaks Correct Answer Incorrect Answer
B Race conditions Correct Answer Incorrect Answer
C Stack overflow Correct Answer Incorrect Answer
D Infinite loops Correct Answer Incorrect Answer
E Deadlock Correct Answer Incorrect Answer

Solution

When multiple threads access and modify shared data concurrently, a race condition can occur. This means the final outcome depends on the non-deterministic order in which the threads execute their operations, potentially leading to incorrect results. Synchronization mechanisms (like locks or mutexes) are needed to prevent race conditions.

Practice Next
ask-question