Subj : Re: Memory Barriers, Compiler Optimizations, etc. To : comp.programming.threads From : Neill Clift [MSFT] Date : Mon Feb 07 2005 01:11 pm "SenderX" wrote in message news:v5GdnRpcBK9IXJrfRVn-vA@comcast.com... >> > Here are some questions that need clarification in your documentation: >> > >> > 1. Can external data depend on the return value from a failed CAS? >> >> I don't undestand what your asking. > > static CMyData my_data; > static volatile LONG flag = 0; > > > my_data.setup(); > InterlockedCompareExchange( &flag, 1, 0 ); > > > AnotherConcurrentThread > ------------------ > > while ( ! InterlockedCompareExchange( &flag, 0, 0 ) ) { Sleep( 0 ); _asm > pause; } > // flag is set > my_data.use(); > > > > Is this safe? > Yes. As another poster mentions a less contrived example would be using say InterlockedCompareExchange to do one time initialization and if the call fails using the return as the initialized value. I would consider it a bug if we didn't honour this and push for it to be fixed in current platforms. .