Skip to main content
Post Undeleted by Nathaniel
added 75 characters in body
Source Link
Nathaniel
  • 17k
  • 2
  • 30
  • 55

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$.

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$, 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$ and the test is satisfied
  • Thread $u$ reads $i = 1$;
  • Thread $t$ executes $10$ loops in total; counter = 20
  • Thread $u$ writes $i = 2$;
  • Thread $t$ tests if $i < 10$ and the test is satisfied
  • Thread $t$ reads $i = 2$;
  • Thread $u$ executes $9$ loops in total; counter = 29
  • Thread $t$ writes $i = 3$;
  • Thread $t$ executes $1$ loop; counter = 110
  • Both tests $i < 10$ are false and both threads exit.

The value $110$ is $10 + 10 + 9 + 9 + … + 1 + 1 = 10\times 11$.

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 $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$ enters the loop and reads $i = 1$;
  • Thread $t$ executes $10$ loops in total; counter = 20
  • Thread $u$ writes $i = 2$;
  • Thread $t$ enters the 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 $2$ loops in total; counter = 64
  • Thread $u$ writes $i = 10$;
  • Thread $u$ finishes the loop; counter = 65
  • Both tests $i < 10$ are false and both threads exit.

The value $65$ is $10 +10 + 9 + 8 + … + 2 +1$.

Post Deleted by Nathaniel
Source Link
Nathaniel
  • 17k
  • 2
  • 30
  • 55

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$, 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$ and the test is satisfied
  • Thread $u$ reads $i = 1$;
  • Thread $t$ executes $10$ loops in total; counter = 20
  • Thread $u$ writes $i = 2$;
  • Thread $t$ tests if $i < 10$ and the test is satisfied
  • Thread $t$ reads $i = 2$;
  • Thread $u$ executes $9$ loops in total; counter = 29
  • Thread $t$ writes $i = 3$;
  • Thread $t$ executes $1$ loop; counter = 110
  • Both tests $i < 10$ are false and both threads exit.

The value $110$ is $10 + 10 + 9 + 9 + … + 1 + 1 = 10\times 11$.