Non-locking data structures in C ++ Compare and Swap Routine

In this article: Locked Data Structures ( pdf ) displays the following Compare and Replace:

template <class T>
bool CAS(T* addr, T exp, T val)
{
  if (*addr == exp)
  {
    *addr = val;
    return true;
  }
  return false;
}

And then says

The whole procedure is atomic

But how is that so? Is it not possible for any other actor to change the meaning addrbetween ifand assignment? In this case, considering that all the code uses this CAS-fundamental, the next time it will be found in a sense "expected", and this is not so. However, this does not change the fact that this can happen, in which case it is still atomic? How about another actor returning, even when he has changed, to be rewritten by that actor? If this cannot happen, then why?

I want to believe the author, so what am I missing here? I think this should be obvious. My apologies in advance if this seems trivial.

+3
source share
1 answer

, "-". -, .

+8

Source: https://habr.com/ru/post/1743088/


All Articles