Subj : CAS question To : comp.programming.threads From : Toby Douglass Date : Fri Mar 25 2005 07:51 pm 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 ); So for example, one might loop like this; while( my_ticket != InterlockedCompareExchange(current_ticket, my_ticket, my_ticket) ); My question is to do with the arguments to the CAS function call. I had thought that it would be invalid to perform any in-line operations upon the arguments to the function, since that would have to be done before or after the actual atomic instruction was executed in the machine code, and so would not be atomic. So for example, this; while( count+1 != InterlockedCompareExchange(&counter, count+1, my_count) ); would be invalid, since the "counter+1" would in fact occur *before* the CAS instruction. 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? -- Toby .