如何在 C 中进行原子增量和获取?
我正在寻找一种方法来自动增加短路,然后返回该值。我需要在内核模式和用户模式下执行此操作,因此它是在 Linux 下、Intel 32 位架构上用 C 语言编写的。不幸的是,由于速度要求,互斥锁不是一个好的选择。
还有其他方法可以做到这一点吗?此时,唯一可用的选项似乎是内联一些程序集。如果是这样的话,有人可以指出我适当的说明吗?
I'm looking for a way to atomically increment a short, and then return that value. I need to do this both in kernel mode and in user mode, so it's in C, under Linux, on Intel 32bit architecture. Unfortunately, due to speed requirements, a mutex lock isn't going to be a good option.
Is there any other way to do this? At this point, it seems like the only option available is to inline some assembly. If that's the case, could someone point me towards the appropriate instructions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GCC
__atomic_*
内置函数从 GCC 4.8 开始,
__sync
内置函数已被弃用,取而代之的是__atomic
内置:https://gcc.gnu。 org/onlinedocs/gcc-4.8.2/gcc/_005f_005fatomic-Builtins.html它们实现了 C++ 内存模型,并且
std::atomic
在内部使用它们。以下 POSIX 线程示例在 x86-64 上与
++
一致失败,但始终与_atomic_fetch_add
配合使用。main.c
编译并运行:
反汇编分析位于:How我可以用纯 C 启动线程吗?
在 Ubuntu 18.10、GCC 8.2.0、glibc 2.28 中测试。
C11
_Atomic
在 5.1 中,上述代码适用于:
glibc 2.28 中添加了 C11
threads.h
,它允许您在没有 POSIX 的纯 ANSI C,最小可运行示例: How do我用纯 C 启动线程?GCC
__atomic_*
built-insAs of GCC 4.8,
__sync
built-ins have been deprecated in favor of the__atomic
built-ins: https://gcc.gnu.org/onlinedocs/gcc-4.8.2/gcc/_005f_005fatomic-Builtins.htmlThey implement the C++ memory model, and
std::atomic
uses them internally.The following POSIX threads example fails consistently with
++
on x86-64, and always works with_atomic_fetch_add
.main.c
Compile and run:
Disassembly analysis at: How do I start threads in plain C?
Tested in Ubuntu 18.10, GCC 8.2.0, glibc 2.28.
C11
_Atomic
In 5.1, the above code works with:
And C11
threads.h
was added in glibc 2.28, which allows you to create threads in pure ANSI C without POSIX, minimal runnable example: How do I start threads in plain C?GCC 支持原子操作:
gcc 原子
GCC supports atomic operations:
gcc atomics