A race condition occurs when two or more threads perform an operation, and the result of the operation depends on unpredictable timing factors; specifically, when each thread executes and waits and when each thread completes the operation.
For example, if two threads execute routines and each increments the same variable (such as X = X + 1), the variable could be incremented twice and one of the threads could use the wrong value. For example:
The value of X is different from what it was when Thread A incremented it, and the program's behavior is incorrect.
Race conditions result from lack of (or ineffectual) synchronization. To avoid race conditions, ensure that any variable modified by more than one thread has only one mutex associated with it, and ensure that all accesses to the variable are made while holding that mutex.
See Section 3.6.4 for another example of a race condition.