This topic has not yet been rated - Rate this topic

MemoryBarrier macro

Applies to: desktop apps | Metro style apps

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.

Syntax

void MemoryBarrier(void);

Parameters

This macro has no parameters.

Return value

This macro does not return a value.

Remarks

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
#endif

#ifdef _IA64_
#define MemoryBarrier __mf
#endif

// x86

FORCEINLINE
VOID
MemoryBarrier (
    VOID
    )
{
    LONG Barrier;
    __asm {
        xchg Barrier, eax
    }
}

Requirements

Minimum supported client

Windows Vista

Minimum supported server

Windows Server 2003

Header

Winnt.h (include Windows.h)

See also

Interlocked Variable Access

 

 

Send comments about this topic to Microsoft

Build date: 3/7/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ