Subj : Re: A question about atomic_ptr To : comp.programming.threads From : Peter Dimov Date : Tue Apr 19 2005 03:26 am Chris Thomasson wrote: >> mfence fails with exception C000001E, "an attempt was made to >> execute an invalid lock sequence". Athlon XP 2800+. >> >> > > Try replacing the inline assembly with this: > > > __asm__ __volatile__ ("lock; addl $0, 0(%%esp)" : : : "memory"); > > > __asm__ __volatile__ ("mfence" : : : "memory"); This won't work as I don't compile with GCC, but if you are concerned about compiler reordering, this is not an issue. I've looked at the disassembly and it's correct. The mfence instruction just causes an exception. The program that I'm running is at the end. Here's the relevant part: 0040103E mov eax,1DCD6500h { add dword ptr [esp], 0 00401043 add dword ptr [esp],0 mfence 00401047 mfence for( int i = 0; i < n; ++i ) { __asm 0040104A dec eax 0040104B jne main+43h (401043h) } } and here's the exception: Unhandled exception at 0x00401047 in testbed.exe: 0xC000001E: } An attempt was made to execute an invalid lock sequence. Complete program: #include #include int main() { int const n = 500000000; timeBeginPeriod( 1 ); DWORD t = timeGetTime(); for( int i = 0; i < n; ++i ) { __asm { lock add dword ptr [esp], 0 } } printf( "lock prefix time: %d\n", timeGetTime() - t ); t = timeGetTime(); for( int i = 0; i < n; ++i ) { __asm { add dword ptr [esp], 0 mfence } } printf( "mfence time: %d\n", timeGetTime() - t ); timeEndPeriod( 1 ); } .