C ++中的内存障碍示例是什么? [英] What are examples of memory barriers in C++?

查看:58
本文介绍了C ++中的内存障碍示例是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到C ++ 11互斥锁不是void lock() volatile.编译器如何知道哪些功能是内存障碍,哪些不是?即使所有功能障碍都不易变,它们是否也存在障碍?什么是鲜为人知的记忆障碍和人人都应该知道的记忆障碍?

I see C++11 mutexes lock is not void lock() volatile. How does the compiler know which functions are memory barriers and which are not? Are all functions barriers even if they are not volatile? What are some less known memory barriers and memory barriers everyone should know?

推荐答案

std::mutex的实际实现是这样的,即编译器不会执行非法的重新排序,不会消除变量加载,并且会确保锁变量是原子访问的,并且CPU为锁的获取和释放执行必要的内存屏障.

The actual implementation of your std::mutex will be such that the compiler doesn't perform illegal reordering, doesn't elide variable loads, and it will ensure that the lock variable is accessed atomically and that the CPU performs the necessary memory barriers for lock acquisition and release.

要确保此操作需要做多少工作的细节因平台而异,但是您的库实现将做正确的事.

The details of how much work needs to be done to ensure this vary from platform to platform, but your library implementation will Do The Right Thing.

这篇关于C ++中的内存障碍示例是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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