根据Linux内核中的atomic_ops.txt,
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! ***
如果你必须使用障碍,那么有什么用?
答案 0 :(得分:0)
atomic_t
和'普通'变量之间的区别在于前者允许两个原子更新(即,其他线程看到旧值或新值,但没有中间状态)和原子读取和更新操作。
换句话说,atomic_t
允许多个线程访问相同的变量。
需要内存障碍来处理不同变量之间的依赖关系。