lundi 29 août 2016

for a multithreaded singled linked list, is "p && !head.compare_exchange_weak(p,p->next)" atomic?

The usual implementation for popping an element in a stack data-structure for multi-threading is:

void pop()
{
  auto p = head.load();
  while( p && !head.compare_exchange_weak(p,p->next) )
    {}
}

The 'while idiom' is subject to the fact that "these operators (in their built-in form) do not evaluate the second operand if the result is known after evaluating the first."(cf. http://ift.tt/1QOSO1w)

I am wondering why we can consider the whole evaluation of the expression

p && !head.compare_exchange_weak(p,p->next)

as atomic and if not wouldn't it be possible that 'p->next' is invalid?

Thanks

Aucun commentaire:

Enregistrer un commentaire