問題描述
我閱讀了英特爾架構的英特爾優化指南指南".
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 的更改可能是全局可見的(例如,對于在其他處理器上運行的線程/進程).在兩個語句之間放置圍欄將確保寫入發送到 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!