The largest should be self-help: 25, with 5 increments from 5 threads.
Totally and completely wrong. No matter what they say, this should never happen (at least about things related to flows), period.
int tmp = n; tmp = tmp + 1; n = tmp;
Imagine a processor that did not have an increase operation but had an effective add 10 operation and an effective subtract nine operation. On such a processor, tmp = tmp + 1; can be optimized to tmp += 10; tmp -= 9; tmp += 10; tmp -= 9; . The compiler can also fully optimize tmp by working on n .
Thus, this code can become equivalent:
n += 10; n -= 9;
Now imagine this happening: all five threads add 10, so n now 50. The first thread reads 50, the other four threads subtract 9. The first thread subtracts 9 from 50, which it reads and writes 41. Therefore, when everything is done, n is 41.
So, that which is claimed to be self-evident, completely false. The one who wrote this does not understand the threads in C.
if each thread writes 1, then the final value cannot be magical with something else
Also completely and completely false. Consider a processor that writes 1, first writing 0, and then increasing the value. If this happens on two cores, the end result may be 2. This tutorial was written by someone who fundamentally does not understand threads and undefined behavior.
(I suppose this tutorial is not limited to any particular context in which what it says is true. For example, it can use the βC-likeβ code as a form of assembly language, and it can make assumptions about platforms , in which aligned integers have specific guarantees, but if so, what he teaches is not translated into C code at all and will only apply to people who write assembly code on processors whose rules comply with the textbook's assumptions.)