I read one article, describing the ABA problem, but there is something, that I can’t understand. I have source code, which fails to work and it is similar to the example in article, but I don’t understand the problem. Here is the article
http://fara.cs.uni-potsdam.de/~jsg/nucleus/index.php?itemid=6
It says: While the actual value of head_ is the same (a) the next_ pointer is NOT
But how can it be? If two structure objects
struct node {
node *next;
data_type data;
};
“head_” and “current” point to the same area in memory, how can head_->next and current->next point to different?
It also says: The last operation, the compare-and-swap by foo SUCCEEDS when it should not.
Then what should it do? Load the same address and try again? What is the difference?
Currently in my code I have similar situation, where I do CompareAndSwap on the object, which might be changed by another thread to the object with similar address
deleted.compare_exchange_strong(head, 0);
but if changed object is well initialized and it’s next pointer contain pointer to initialized object then what is the problem?
Thanks in advance.
They do not; but the code needs that both
headandhead->nextare stable while thepopmethod runs – but the CAS only ensures this forhead. It silently assumeshead->nextwon’t changed without changinghead, which is false. So it reads something ascurrent->nextand a while later, it changes.Yes. The method needs to wait (or keep trying) until noone messes with the structure under its hands.
Could be anything. Violations of classes’ invariants, double frees/memory leaks, losses of data etc.