C语言 如何在C中进行原子增量和获取?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2353371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do an atomic increment and fetch in C?
提问by Bryan
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.
我正在寻找一种方法来原子地增加一个短,然后返回该值。我需要在内核模式和用户模式下都这样做,所以它是在 C 中,在 Linux 下,在英特尔 32 位架构上。不幸的是,由于速度要求,互斥锁不是一个好的选择。
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?
有没有其他方法可以做到这一点?在这一点上,似乎唯一可用的选择是内联一些程序集。如果是这种情况,有人可以指出我的适当说明吗?

