Related: Does the Intel Memory Model make SFENCE and LFENCE redundant?
sfence
has no real effect unless you're using NT stores1. If you NT-store data and then a pointer to that data (or a "ready" flag), a reader can see the old value for the data even if they see the new pointer / flag value. sfence
can be used to ensure that the two stores become observable in program order.
lfence
is useless for memory ordering unless you're doing NT loads from a WC memory region (like video RAM). You'll have a very hard time creating a case where commenting it out creates a detectable different in memory ordering.
The main use for lfence
is to serialize execution, not memory. See Understanding the impact of lfence on a loop with two long dependency chains, for increasing lengths
Since you asked about C not just asm, there's a related answer about when you should use _mm_sfence()
and other intrinsics. When should I use _mm_sfence _mm_lfence and _mm_mfence (usually you really only need asm("" ::: "memory");
unless NT stores are in flight, because blocking compile-time reordering gives you acq / rel ordering without any runtime barrier instructions.)
Footnote 1: That's true for normal WB (WriteBack) memory cacheability settings. In user-space under a normal OS, that's what you always have unless you did something very special.
For other memory types (MTRR or PAT settings): NT stores on uncacheable memory have no special effect, and are still strongly ordered. NT stores on WC, WB, or WT memory (or normal stores to WC memory) are weakly ordered and make it useful to use sfence
before storing a buffer_ready
flag for another thread.
SSE4.1 movntdqa
loads from WB memory are not weakly ordered. Unlike stores, it doesn't override the memory type's ordering semantics. On current CPUs, nothing special happens at all on WB memory; they're just a less-efficient movdqa
laod. Only use them on WC memory.
mfence
? You actually need to write many tests to check every property of all of the three fence instructions for Intel and AMD processors, which is going to take a lot of effort. – Spaceport