加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

Linux中的atomic_t

发布时间:2020-12-13 18:50:21 所属栏目:Linux 来源:网络整理
导读:我正在研究Robert Love的 Linux内核开发 Linux内核. 如您所知,本书使用旧版Linux.它是2.6版本 atomic_t有“volatile int counter”.但是新Linux版本的atomic_t的“int counter”并不易变.为什么这种挥发性已经消失了? 解决方法 因为volatile变量不是原子变
我正在研究Robert Love的 Linux内核开发 Linux内核.

如您所知,本书使用旧版Linux.它是2.6版本

atomic_t有“volatile int counter”.但是新Linux版本的atomic_t的“int counter”并不易变.为什么这种挥发性已经消失了?

解决方法

因为volatile变量不是原子变量.使用volatile的唯一要点是防止可能的编译器优化,这与防止不需要的并发访问不同.

在这方面,挥发性物质的使用几乎永远不正确.

你可以在Semantics and Behavior of Atomic and Bitmask Operations阅读更多相关信息.

引用其中的一小部分:

* WARNING: atomic_read() and atomic_set() DO NOT IMPLY BARRIERS! *

Some architectures may choose to use the volatile keyword,barriers,or inline
assembly to guarantee some degree of immediacy for atomic_read() and
atomic_set(). This is not uniformly guaranteed,and may change in the future,
so all users of atomic_t should treat atomic_read() and atomic_set() as simple
C statements that may be reordered or optimized away entirely by the compiler
or processor,and explicitly invoke the appropriate compiler and/or memory
barrier for each use case. Failure to do so will result in code that may
suddenly break when used with different architectures or compiler
optimizations,or even changes in unrelated code which changes how the
compiler optimizes the section accessing atomic_t variables.

* YOU HAVE BEEN WARNED! *

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读