MemoryBarrier function (winnt.h)

Creates a hardware memory barrier (fence) that prevents the CPU from re-ordering read and write operations. It may also prevent the compiler from re-ordering read and write operations.


void MemoryBarrier();

Return value



Use this macro or the interlocked functions when the order of memory read and write operations is critical for program operation.

The _ReadBarrier, _WriteBarrier, and _ReadWriteBarrier compiler intrinsics prevent compiler re-ordering only. With Visual Studio 2003, volatile to volatile references are ordered; the compiler will not re-order volatile variable access. With Visual Studio 2005, the compiler also uses acquire semantics for read operations on volatile variables and release semantics for write operations on volatile variables (when supported by the CPU). For more information, see Synchronization and Multiprocessor Issues.

This macro can be called on all processor platforms where Windows is supported, but it has no effect on some platforms. The definition varies from platform to platform. The following are some definitions of this macro in Winnt.h.

#ifdef _AMD64_
#define MemoryBarrier __faststorefence

#ifdef _IA64_
#define MemoryBarrier __mf

// x86

MemoryBarrier (
    LONG Barrier;
    __asm {
        xchg Barrier, eax


Requirement Value
Minimum supported client Windows Vista [desktop apps | UWP apps]
Minimum supported server Windows Server 2003 [desktop apps | UWP apps]
Target Platform Windows
Header winnt.h (include Windows.h)

See also

Interlocked Variable Access