Ruben Soto wrote:The result is not guaranteed because you have 3 different locks at play. What you can guarantee about the result is that it will be of the form XYZ, where 6 <= X <= 10, 8 <= Y <= 10, Z = 10, and X <= Y <= Z.
Actually no... even this can't be guaranteed. Keep in mind that (1) the increment operator is not atomic. And (2) you can have true parallelism with multiple processors / cores. For the second case, if you run this on a computer with multiple processors, you can actually lose increments -- as two processors will start will the same value and increment it in parallel. This means that it is possible to have a Z which is less than 10.
You can actually also lose increments on a single processor too -- but the chances of a timeslice, occuring in-between the instructions of the increment, is low, of course.
Henry