I'm trying to learn and better understand multithreading, but I hung up on the behavior of atomic functions like fetch-and-add. In the specific case of fetch-and-add, I understand that the value (let x, which is currently 5) is added to the increment value (say 3), the resulting sum (8) is written to x in memory, but the old value is returned (5).
There are several other such functions in different places (such as the atomic functions of OpenGL, Java AtomicIntegers, and many other areas) that behave this way. But I donβt understand why the place in the code wants to be written into memory and still return the value that it wants to change in the first place. Can anyone help shed some light on this?
source share