Interlocked.MemoryBarrierProcessWide Interlocked.MemoryBarrierProcessWide Interlocked.MemoryBarrierProcessWide Interlocked.MemoryBarrierProcessWide Method


提供整個處理序範圍的記憶體屏障,以確保來自任何 CPU 的讀取和寫入都無法穿越屏障。Provides a process-wide memory barrier that ensures that reads and writes from any CPU cannot move across the barrier.

 static void MemoryBarrierProcessWide();
public static void MemoryBarrierProcessWide ();
static member MemoryBarrierProcessWide : unit -> unit
Public Shared Sub MemoryBarrierProcessWide ()


方法與 "normal" MemoryBarrier方法不同,如下所示: MemoryBarrierProcessWideThe MemoryBarrierProcessWide method differs from the "normal" MemoryBarrier method as follows:

  • 一般記憶體屏障可確保目前 CPU 的讀取和寫入無法跨屏障移動。The normal memory barrier ensures that the reads and writes from the current CPU can't move across the barrier. 整個進程的記憶體屏障可確保在進程中使用之任何 CPU 的任何讀取或寫入,都無法在屏障之間移動。The process-wide memory barrier ensures that any read or write from any CPU being used in the process can't move across the barrier.

  • 如果存取資料的每個執行緒都使用阻礙,一般記憶體屏障會允許合理的共用存取。The normal memory barrier allows reasonable shared access if every thread accessing the data uses barriers. 整個進程記憶體屏障會強制其他 cpu與進程記憶體進行同步處理(例如,清除寫入緩衝區並同步處理讀取緩衝區)。The process-wide memory barrier forces other CPUs to synchronize with process memory (for example, to flush write buffers and synchronize read buffers). 這允許在某些執行緒上進行非連鎖作業,而且仍然具有合理的共用存取權。This allows for non-interlocked operations on some threads and still have reasonable shared access.

  • 一般記憶體屏障會有極少的額外負荷;正常連鎖作業的成本可能少於一百個週期。The normal memory barrier imposes very little overhead; normal interlocked operations probably cost fewer than a hundred cycles. 整個進程的記憶體屏障非常昂貴。The process-wide memory barrier is very expensive. 它必須強制執行進程中的每個 CPU,並以數千個迴圈的可能成本來進行。It has to force every CPU in the process do to something, at a probable cost of thousands of cycles.

MemoryBarrierProcessWide方法也會受到無鎖定程式設計的所有微妙差異。The MemoryBarrierProcessWide method also suffers from all the subtleties of lock-free programming. 不過,當您實際需要呼叫它時,這個方法可能非常有用,這應該很罕見。Nevertheless, this method can be extremely useful when you actually need to call it, which should be rare.

這個方法會包裝對 Windows 和sys_membarrier on Linux 上的FlushProcessWriteBuffers呼叫。This method wraps a call to FlushProcessWriteBuffers on Windows and sys_membarrier on Linux.