While I was initialy convinced by the two previous answers, a colleague of mine showed me the light about this exercise.
He claims that the maximum value of the counter is $110$$65$, not $20$.
As proof, consider the following execution :
- Thread $t$ reads $i = 0$;
counter = 0
- Thread $u$ executes $10$ loops in total;
counter = 10
- Thread $t$ writes $i = 1$;
- Thread $u$ tests if $i < 10$ andenters the test is satisfied
- Thread $u$loop and reads $i = 1$;
- Thread $t$ executes $10$ loops in total;
counter = 20
- Thread $u$ writes $i = 2$;
- Thread $t$ tests if $i < 10$ andenters the test is satisfied
- Thread $t$loop and reads $i = 2$;
- Thread $u$ executes $9$ loops in total;
counter = 29
- Thread $t$ writes $i = 3$;
- …
- Thread $t$ writes $i = 9$;
- Thread $u$ enters the loop and reads $i = 9$;
- Thread $t$ executes $1$$2$ loops in total;
counter = 64
- Thread $u$ writes $i = 10$;
- Thread $u$ finishes the loop;
counter = 11065
- Both tests $i < 10$ are false and both threads exit.
The value $110$$65$ is $10 + 10 + 9 + 9 + … + 1 + 1 = 10\times 11$$10 +10 + 9 + 8 + … + 2 +1$.