Subj : Re: CAS question To : comp.programming.threads From : Joe Seigh Date : Fri Mar 25 2005 04:39 pm On Fri, 25 Mar 2005 19:51:32 -0000, Toby Douglass wrote: > I'm currently thinking about lockless locking. > > One of the fundamental operations used is Compare-And-Swap. > > The Windows API for this is; > > LONG InterlockedCompareExchange( LONG volatile *Destination, LONG > Exchange, LONG Comperand ); > (bad example deleted) > > However, I've just seen this page; > > http://www.cs.rochester.edu/u/scott/synchronization/pseudocode/queues.ht > ml > > Which seems to imply this in-line work *can* be done. > > 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); With the latter api (note new and old/compare parms are reversed) you can do oldcount = count; while (!cas(&count, &oldcount, oldcount + 1)); -- Joe Seigh .