What is a Race Condition? Provide a conceptual example of how it can lead to data inconsistency.
Last updated
A race condition occurs when two or more threads access shared data concurrently and the final result depends on the unpredictable order of their execution. It arises because operations that look atomic in source code are actually multiple machine steps that can interleave.
Why it happens:
A statement like
count++is really read, modify, write: three steps that can be interrupted mid-way.If two threads interleave those steps, one update can silently overwrite the other (a lost update).
Conceptual example (two threads increment count = 5):
Thread A reads count = 5.
Thread B reads count = 5 (before A writes back).
A computes 6 and writes 6; B computes 6 and writes 6.
Result is 6, but two increments should have given 7: one update was lost.
The fix: Serialize access to the shared data with synchronization (mutex, atomic operation) so the read-modify-write is indivisible.
- https://www.reddit.com/r/cscareeradvice/new/
- https://www.reddit.com/r/macbook/comments/1jg7fl6/24gb_ram_enough_for_software_engineering/
- https://www.reddit.com/r/infosys/comments/1t71add/anyone_received_round_2_mails/
- https://www.reddit.com/r/cscareeradvice/best/
- https://www.reddit.com/r/LeetcodeChallenge/
- https://www.reddit.com/r/infosys/comments/1t71i55/infosys_specialist_programmer_april_26_1st_round/
- https://www.reddit.com/r/programming/comments/1hqo19u/static_search_trees_40x_faster_than_binary_search/