自然对齐+ volatile = C ++ 11中的原子? [英] Natural alignment + volatile = atomic in C++11?

查看:85
本文介绍了自然对齐+ volatile = C ++ 11中的原子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1)以下是自然对齐指针的声明:

1) Is the following declaration of a naturally aligned pointer:

alignas(sizeof(void *)) volatile void * p;

相当于

std::atomic<void *> 

在C ++ 11中?

2)更准确地说,假设这种类型的指针将以与C ++ 11中的std :: atomic相同的方式工作是否正确?

2) Saying more exactly, is it correct to assume that this type of pointer will work in the same way as std::atomic in C++11?

推荐答案

不,volatile不保证该位置将被原子地读写,只是编译器无法优化多个读写。

No, volatile does not guarantee that the location will be written or read atomically, just the the compiler can't optimise out multiple reads and writes.

在某些体系结构上,如果对齐正确,处理器将自动进行读写操作,但这不是通用的,甚至不能通过一系列处理器来保证。在可能的情况下,原子的内部实现将利用体系结构功能和原子指令修饰符,因此,如果您是指原子,为什么 使用原子?

On certain architectures, the processor will atomically read or write if aligned correctly, but that is not universal or even guaranteed through a family of processors. Where it can, the internal implementation of atomic will take advantage of architectural features and atomic instruction modifiers, so why not use atomic, if you mean atomic?

这篇关于自然对齐+ volatile = C ++ 11中的原子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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