The volatile variables are said to be directly read/written to main memory. And, it is said that if we have one writer and multiple readers for volatile variables, then we won't have any race conditions. If we consider the following code snippet:
If we have one
thread say T1 which is performing above code of i++ and some other threads T2, T3, T4 etc are reading the value of i. In that case, suppose T1 reads the value as 1, then increments it by 1 to make it 2 but haven't stored it yet in main memory and in between the other thread say T2 will read the value as 1 only. And, then thread T1 will write the value to main memory. In this scenario, don't we have a race condition as thread T2 has read some stale value which was being in update by T1?