Subj : Re: CAS question To : comp.programming.threads From : Joe Seigh Date : Fri Mar 25 2005 05:14 pm On Fri, 25 Mar 2005 22:03:55 -0000, Toby Douglass wrote: > jseigh_01@xemaps.com wrote: >> On Fri, 25 Mar 2005 19:51:32 -0000, Toby Douglass wrote: > >> (bad example deleted) > > Why was it a bad example? It was incorrect usage. The example I gave was the correct usage. > >> > My question is: is this so? can I perform any in-line operations on the >> > arguments to CAS? >> >> Yes but Microsoft's api is a little awkward. It's better if you have an >> api that returns a boolean and updates the compare value instead. With >> Microsoft's api you need to do something like >> >> oldcount = count; >> while ((oldcount = InterlockedCompareExchange(&count, oldcount + 1, cmpcount = oldcount)) != cmpcount); > > The "cmpcount = oldcount" assignment for the third argument is atomic > because it's one of the arguments to InterlockedCompareExchange()? > > It might be that within the algorithm in use, that assignment doesn't > need to be atomic, which is fine, but surely that assignment is cannot > be atomic because it is performed as the argument to the function call. > The argument expressions are evaluated before the function is called. They don't have to be atomic or anything. -- Joe Seigh .