为什么在整个 std::atomic 中使用 volatile 限定符? [英] Why is the volatile qualifier used through out std::atomic?

查看:35
本文介绍了为什么在整个 std::atomic 中使用 volatile 限定符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我从 Herb Sutter 中读到的内容其他 你会认为 volatile 和并发编程是完全正交的概念,至少就 C/C++ 而言是这样.

然而,在 GCC 实现中的所有std::atomic 的成员函数具有 volatile 限定符.在 Anthony Williams 的 implementation std 中也是如此::原子.

那有什么关系,我的atomic<> 变量是否需要volatile?

解决方案

为什么 volatile 限定符在整个 std::atomic 中使用?

这样 volatile 对象也可以是原子的.请参阅此处:>

相关报价是

<块引用>

函数和操作被定义为与 volatile 对象一起工作,因此应该是 volatile 的变量也可以是原子的.但是,原子性不需要 volatile 限定符.

我的atomic<>变量是否需要volatile?

不,原子对象不必是可变的.

From what I've read from Herb Sutter and others you would think that volatile and concurrent programming were completely orthogonal concepts, at least as far as C/C++ are concerned.

However, in GCC implementation all of std::atomic's member functions have the volatile qualifier. The same is true in Anthony Williams's implementation of std::atomic.

So what's deal, do my atomic<> variables need be volatile or not?

解决方案

Why is the volatile qualifier used throughout std::atomic?

So that volatile objects can also be atomic. See here:

The relevant quote is

The functions and operations are defined to work with volatile objects, so that variables that should be volatile can also be atomic. The volatile qualifier, however, is not required for atomicity.

Do my atomic<> variables need to be volatile or not?

No, atomic objects don't have to be volatile.

这篇关于为什么在整个 std::atomic 中使用 volatile 限定符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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