简体   繁体   English

Linux中的atomic_t

[英]atomic_t in Linux

I'm studying Linux kernel with Linux Kernel Development by Robert Love. 我正在研究Robert Love的Linux内核开发Linux内核。

As you know, this book uses old version of Linux. 如您所知,本书使用旧版Linux。 It's in 2.6 version 它是2.6版本

atomic_t has "volatile int counter". atomic_t有“volatile int counter”。 But newly Linux version's atomic_t has "int counter" not volatile. 但是新Linux版本的atomic_t的“int counter”并不易变。 Why this volatile has erased? 为什么这种挥发性已经消失了?

Because volatile variables are not atomic variables. 因为volatile变量不是原子变量。 The only point in using volatile is to prevent possible compiler optimisations, which is not the same as preventing unwanted concurrent access. 使用volatile的唯一要点是防止可能的编译器优化,这与防止不需要的并发访问不同。

In that regard, the use of volatile is almost never correct. 在这方面, volatile的使用几乎从不正确。

You can read more about it in Semantics and Behavior of Atomic and Bitmask Operations . 您可以在原子和位掩码操作的语义和行为中阅读更多相关信息。

Quoting a small part of it: 引用其中的一小部分:

* WARNING: atomic_read() and atomic_set() DO NOT IMPLY BARRIERS! *警告:atomic_read()和atomic_set()不要暗示障碍! * *

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(). 某些体系结构可能会选择使用volatile关键字,障碍或内联汇编来保证atomic_read()和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. 这不是一致的保证,并且可能在将来发生变化,因此atomic_t的所有用户都应将atomic_read()和atomic_set()视为可以完全由编译器或处理器重新排序或优化的简单C语句,并显式调用相应的每个用例的编译器和/或内存屏障。 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. 如果不这样做,将导致代码在与不同的体系结构或编译器优化一起使用时可能突然中断,甚至会导致无关代码的更改,从而改变编译器优化访问atomic_t变量的部分的方式。

* YOU HAVE BEEN WARNED! * 你被警告了! * *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM