简体   繁体   English

需要帮助来了解“ ABA”问题

[英]Need help in understanding the “ABA” problem

I read one article, describing the ABA problem, but there is something, that I can't understand. 我读了一篇描述ABA问题的文章,但是有些事情我听不懂。 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 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 它说:尽管head_的实际值是相同的(a)next_指针不是

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? “ head_”和“ current”指向内存中的同一区域,head _-> next和current-> next如何指向不同?

It also says: The last operation, the compare-and-swap by foo SUCCEEDS when it should not. 它还说:最后一个操作,不应该由foo SUCCEEDS执行比较和交换。

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 当前在我的代码中,我有类似的情况,我在对象上执行CompareAndSwap,这可能会被另一个线程更改为地址相似的对象

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? “ head_”和“ current”指向内存中的同一区域,head _-> next和current-> next如何指向不同?

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 . 但是代码需要在pop方法运行时headhead->next都必须稳定-但是CAS只为head保证这一点。 It silently assumes head->next won't changed without changing head , which is false. 它默默地假设head->next在不更改head情况head->next不会更改,这是错误的。 So it reads something as current->next and a while later, it changes. 因此它读取的内容为current->next ,不久之后,它发生了变化。

It also says: The last operation, the compare-and-swap by foo SUCCEEDS when it should not. 它还说:最后一个操作,不应该由foo SUCCEEDS执行比较和交换。

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. 违反类的不变性,两次释放/内存泄漏,数据丢失等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM