Say I have 3 threads, T1, T2 and T3 from an application all starting at the exact same time. They all read the same value from a certain table, row and column. Lets say this value is 50.
1. T1 is fast. Completes in 100ms. Upon completion, it updates the value it read with +5. So, it writes 55 in the database.
2. T2 is a bit slower. Completes in 700ms. And upon completion, it adds +10 to the value it read. So it updates the data with 60.
3. T3 is even slower and completes in 1300ms. On completion this thread adds +15. So it the value becomes 65.
The solution I'm hoping to learn from SO is how does one handle the data consistency in such cases. Since, clearly at the end of T3 the value should be 80 (50 + 5 T1 + 10 T2 + 15 T3) as opposed to 65.
Do let me know if I can make my question any clearer if needed.