First of all, lets clarify the functional difference between
i = i+1
cnt = counter
counter = cnt+1
There is none! Both segments must first read a value from global memory into a register, operate on/with said register and write the result back to global memory. So both execute three instructions; a read, an add, and a write.
With that said, the rest of the problem is an exercise of arranging blocks in a way that one thread "undoes" the work of the other, either for
i or for
For the minimum value of
counter, see the execution flow below. The grey box represents a single iteration of the loop. The value of
counter, after both threads finish, will be 1.
For the maximum value of
coutner, see the execution flow below. Again, the grey boxes represent a single iteration of the loops. If all iterations are executed with the alignment shown, at the end,
counter will be 20.