Subj : Re: CMPXCHG timing To : comp.programming.threads From : Michael Pryhodko Date : Fri Apr 01 2005 04:04 pm > > processor reads value from memory, compares it with EAX and if they are > > different processor WRITES BACK original value!! Shit!! Who and why > > decided to do that??? Until someone explains me reasons behind this > > decision I will consider it as very "bright" and "brilliant" move from > > Intel. > > This is well-documented behavior of the CMPXCHG function. The write is > unconditional. You should not be complaining about this because if you want > atomic behavior, you have to use the LOCK prefix anyway, in which case the > write is harmless. If you don't want atomic behavior, implement the function > yourself without the extra write. I DO NOT WANT ATOMIC BEHAVIOR! Please, read carefully more formal description of algorithm I posted. The whole idea of it was "we do not need LOCK to do lock". Main "feature" of algorithm was that ONLY i-th processor could ever write unique value Pi into lock variable, but with this stupid implementation of CMPXCHG any processor that tries to enter locked lock writes Pi of lock owner every CMPXCHG call. This spoils whole idea. I do not understand why Intel decided to implement CMPXCHG this way, but from all I learned so far I do not know any good "excuse" for that. > By the way, looking at all of your posts on this issue, I think you're > totally barking up the wrong tree. I suggest you to take care next time -- this statement is quite an insult in my culture. > Single instructions without the LOCK > prefix are not atomic with respect to other processors anyway -- if they > were, what would the LOCK prefix be for? And you cannot rely on instruction > timings for synchronization because of things like cache misses, speculative > reads, and so on. Please read carefully IA manual, especially section where it states necessary conditions for simple operations to be atomic. For example store of 32bit value to 4byte aligned memory address is atomic without any LOCK (which is forbidden for 'mov'). And by the way, in this example I took 'cache misses' and 'speculative reads' into account. Read comments carefully and pay attention to sfences/mfences in the code. > I suggest you think carefully about what you're trying to do. Read carefully before answering. ;) Bye. Sincerely yours, Michael. .