$linuxjunkies
>

race condition

also: TOCTOU, time-of-check to time-of-use

A race condition occurs when the outcome of concurrent operations depends on their unpredictable timing or order, causing inconsistent or incorrect results.

A race condition happens when two or more processes or threads access and modify shared resources (like files, memory, or variables) without proper synchronization. The final result becomes unpredictable because it depends on which operation completes first—essentially, the processes are "racing" to modify the resource.

In Linux, race conditions commonly occur in multi-threaded programs or when multiple processes interact with the same file. For example, if two programs simultaneously read a counter from a file, increment it, and write it back, the final value may be incorrect because both read the original value before either writes the update back.

To prevent race conditions, developers use synchronization mechanisms like mutexes, semaphores, atomic operations, or file locks. Without proper locking, unpredictable behavior—ranging from lost data updates to security vulnerabilities—can result.

Related terms