是" ++ QUOT;操作用C原子? [英] Is the "++" operation atomic in C?

查看:94
本文介绍了是" ++ QUOT;操作用C原子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定是否像一个语句:

I am trying to determine whether or not a statement like:

++value;   //assuming "value" is a **global** variable

是一个原子操作。

is an atomic operation.

我需要知道,如果这个计算是能够通过中断服务程序写入到同一个全局变量被打断。

I need to know if this calculation is capable of being interrupted by an Interrupt Service Routine that writes to the same global variable.

推荐答案

在对象,而一个原子型,标准从未++定义为一个原子操作。

On objects without an atomic type, standard never defines ++ as an atomic operation.

C11在stdatomic.h定义的原子类型。
如果你有一个对象,一个原子类型,一个的后缀的和的 preFIX 的操作符 ++ 将定义一个原子操作是:读 - 修改 - 写有memory_order_seq_cst操作
内存序语义。

C11 defines atomic types in stdatomic.h. If you have an object with an atomic type, a postfix and prefix operators ++ will define an atomic operation as: read-modify-write operation with memory_order_seq_cst memory order semantics.

您还可以,如果你想要一个原子增量使用atomic_fetch_add()。

You can also use atomic_fetch_add() if you want an atomic increment.

这篇关于是" ++ QUOT;操作用C原子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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