I read one article, describing the ABA problem, but there is something, that I can't understand. I have source code, which fai开发者_如何学Pythonls 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.
"head_" and "current" point to the same area in memory, how can head_->next and current->next point to different?
They do not; but the code needs that both head
and head->next
are stable while the pop
method runs - but the CAS only ensures this for head
. It silently assumes head->next
won't changed without changing head
, which is false. So it reads something as current->next
and a while later, it changes.
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?
Yes. The method needs to wait (or keep trying) until noone messes with the structure under its hands.
but if changed object is well initialized and it's next pointer contain pointer to initialized object then what is the problem?
Could be anything. Violations of classes' invariants, double frees/memory leaks, losses of data etc.
精彩评论