__sync_bool_compare_and_swap总是返回零

发布于 2025-01-25 00:45:49 字数 1035 浏览 4 评论 0原文

我是否会误解GCC中的内置CAS运营商?

从文档中:

bool __sync_bool_compare_and_swap(类型 *ptr,type od old type type newval,...)

类型__sync_val_compare_and_swap(类型 *ptr,type old oldVal类型newval,...) 这些内置的人进行原子比较和交换。也就是说,如果 *ptr的当前值是旧的,则将newval写入 *ptr。

如果比较成功并且编写了newval,则“ bool”版本将返回true。 “ val”版本在操作之前返回 *ptr的内容。

但是,但是,在我的调试器中:

atomic_cas (ax=0x5555555728b0 <__libc_csu_init>, checkval=0, newval=0) at atomic.c:18
18      int atomic_cas(int *ax, int checkval, int newval) {
(gdb) n
19         return __sync_bool_compare_and_swap(&ax, checkval, newval);
(gdb) p *ax
$1 = 0
(gdb) p checkval
$2 = 0
(gdb) p newval
$3 = 1
(gdb) p *ax == checkval
$4 = 1
(gdb) call atomic_cas(ax, checkval, newval)
$5 = 0

...实际上:

(gdb) call atomic_cas(ax, 0, 1)
$6 = 0
(gdb) call atomic_cas(ax, 1, 0)
$7 = 0
(gdb) call atomic_cas(ax, 1, 1)
$8 = 0

在这里给出了什么?这是GCC中的错误还是我做错了什么?

(请注意:我知道同步_*已弃用,请不要告诉我我应该如何移至较新的原子。这只是一个测试。)

Am I misunderstanding the builtin CAS operators in gcc?

From the docs:

bool __sync_bool_compare_and_swap (type *ptr, type oldval type newval, ...)

type __sync_val_compare_and_swap (type *ptr, type oldval type newval, ...)
These builtins perform an atomic compare and swap. That is, if the current value of *ptr is oldval, then write newval into *ptr.

The “bool” version returns true if the comparison is successful and newval was written. The “val” version returns the contents of *ptr before the operation.

But, yet, in my debugger:

atomic_cas (ax=0x5555555728b0 <__libc_csu_init>, checkval=0, newval=0) at atomic.c:18
18      int atomic_cas(int *ax, int checkval, int newval) {
(gdb) n
19         return __sync_bool_compare_and_swap(&ax, checkval, newval);
(gdb) p *ax
$1 = 0
(gdb) p checkval
$2 = 0
(gdb) p newval
$3 = 1
(gdb) p *ax == checkval
$4 = 1
(gdb) call atomic_cas(ax, checkval, newval)
$5 = 0

... and in fact:

(gdb) call atomic_cas(ax, 0, 1)
$6 = 0
(gdb) call atomic_cas(ax, 1, 0)
$7 = 0
(gdb) call atomic_cas(ax, 1, 1)
$8 = 0

What gives here? Is this a bug in gcc or am I doing something wrong?

(Notice: I'm aware the sync_* are deprecated, please don't tell me how I should move to the newer atomics. I'm also aware that it's silly for this function call to exist - it's just a test.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文