简体   繁体   English

C ++中的内存屏障有哪些例子?

[英]What are examples of memory barriers in C++?

I see C++11 mutexes lock is not void lock() volatile . 我看到C ++ 11互斥锁没有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? 每个人都应该知道哪些鲜为人知的记忆障碍和记忆障碍?

The runtime library has to implement a mutex in a way so that the compiler knows! 运行时库必须以某种方式实现互斥锁,以便编译器知道! The language standard doesn't say anything about how to do this. 语言标准没有说明如何做到这一点。

Likely, it involves a call to some operating system service that works as a memory barrier. 可能,它涉及调用一些作为内存屏障的操作系统服务。 Or the compiler can have an extension, like void _ReadWriteBarrier(); 或者编译器可以有一个扩展名,如void _ReadWriteBarrier();

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. std::mutex的实际实现将使编译器不执行非法重新排序,不会忽略变量加载,并且它将确保以原子方式访问lock变量并且CPU执行必要的内存屏障锁定获取和释放。

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. 需要做多少工作以确保这一点的细节因平台而异,但您的库实现将做正确的事情。

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

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