The key is in the compare_exchange_weak interface, which in this case takes 2 arguments. The first is a reference to the expected value, and the second is the desired value. If the current atom value is not equal to the expected input, it will return false and the expected input will be set to the current value.
So, in this case, setting new_node->next = head . Then, saying that if head is still equal to new_node->next , change it to head . If this is no longer a value, it uses the link to new_node->next to give it the current value of head . Since each iteration of the loop that fails also replaces new_node->next with the current value of head , there is no reading to duplicate this in the body of the loop.
source share