我什么时候应该使用_mm_sfence _mm_lfence和_mm_mfence [英] When should I use _mm_sfence _mm_lfence and _mm_mfence

查看:527
本文介绍了我什么时候应该使用_mm_sfence _mm_lfence和_mm_mfence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了《英特尔架构优化指南》.

I read the "Intel Optimization guide Guide For Intel Architecture".

但是,我仍然不知道何时应该使用

However, I still have no idea about when should I use

_mm_sfence()
_mm_lfence()
_mm_mfence()

有人能解释在编写多线程代码时应何时使用这些代码吗?

Could anyone explain when these should be used when writing multi-threaded code?

推荐答案

注意事项:我不是这方面的专家.我仍在尝试自己学习.但是,由于过去两天没有人答复,因此关于内存隔离栅指令的专家似乎并不多.所以这是我的理解...

Caveat: I'm no expert in this. I'm still trying to learn this myself. But since no one has replied in the past two days, it seems experts on memory fence instructions are not plentiful. So here's my understanding ...

英特尔是弱排序内存系统.这意味着您的程序可以执行

Intel is a weakly-ordered memory system. That means your program may execute

array[idx+1] = something
idx++

,但是在更改为 array 之前,对 idx 的更改可能是全局可见的(例如,对于在其他处理器上运行的线程/进程).将 sfence 放在这两个语句之间将确保将写入发送到FSB的顺序.

but the change to idx may be globally visible (e.g. to threads/processes running on other processors) before the change to array. Placing sfence between the two statements will ensure the order the writes are sent to the FSB.

与此同时,另一个处理器正在运行

Meanwhile, another processor runs

newestthing = array[idx]

可能已为 array 缓存了内存并具有旧副本,但由于缓存未命中而获得了更新的 idx . 解决方案是先使用围栏来确保负载同步.

may have cached the memory for array and has a stale copy, but gets the updated idx due to a cache miss. The solution is to use lfence just beforehand to ensure the loads are synchronized.

本文

This article or this article may give better info

这篇关于我什么时候应该使用_mm_sfence _mm_lfence和_mm_mfence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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