gpt4 book ai didi

c++ - 需要帮助理解 "ABA"问题

转载 作者:搜寻专家 更新时间:2023-10-31 00:21:37 24 4
gpt4 key购买 nike

我读了一篇描述 ABA 问题的文章,但有些东西我无法理解。我有源代码,它无法运行,它类似于文章中的示例,但我不明白这个问题。这是文章

http://fara.cs.uni-potsdam.de/~jsg/nucleus/index.php?itemid=6

它说:虽然 head_ 的实际值是相同的 (a) next_ 指针不是

但怎么可能呢?如果两个结构对象

struct node {
node *next;
data_type data;
};

“head_”和“current”指向内存中的同一个区域,head_->next和current->next怎么指向不同的地方呢?

它还说:最后一个操作,foo 的比较和交换在不应该成功的时候成功了。

那它该怎么办呢?加载相同的地址并重试?有什么区别?

目前在我的代码中我有类似的情况,我在对象上执行 CompareAndSwap,这可能会被另一个线程更改为具有相似地址的对象

deleted.compare_exchange_strong(head, 0);

但是如果改变的对象被很好地初始化并且它的下一个指针包含指向初始化对象的指针那么问题是什么?

提前致谢。

最佳答案

"head_" and "current" point to the same area in memory, how can head_->next and current->next point to different?

他们没有;但代码需要 headhead->nextpop 方法运行时保持稳定 - 但 CAS 仅确保 >头。它默默地假设 head->next 在不更改 head 的情况下不会更改,这是错误的。所以它读取的内容为 current->next,过了一会儿,它发生了变化。

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?

是的。该方法需要等待(或继续尝试)直到没有人弄乱它手下的结构。

but if changed object is well initialized and it's next pointer contain pointer to initialized object then what is the problem?

可以是任何东西。违反类的不变量、双重释放/内存泄漏、数据丢失等。

关于c++ - 需要帮助理解 "ABA"问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4398069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com