如何在C中进行原子增量和获取? [英] How to do an atomic increment and fetch in C?

查看:62
本文介绍了如何在C中进行原子增量和获取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,以原子方式递增一个short,然后返回该值.我需要在内核模式和用户模式下都执行此操作,因此它是在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?

推荐答案

GCC支持原子操作:

GCC supports atomic operations:

gcc原子

这篇关于如何在C中进行原子增量和获取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆