Subj : Re: Memory visibility and MS Interlocked instructions To : comp.programming.threads From : David Schwartz Date : Thu Aug 25 2005 11:43 am "John Hickin" wrote in message news:dekva7$p36$1@zcars129.ca.nortel.com... > LONG volatile fValueHasBeenComputedState = 0; > > void CacheComputedValue() > { > switch (InterlockedExchangeAdd(&fValueHasBeenComputedState,(LONG)1)) > { > case 0; > iValue = ComputeValue(); > InterlockedExchange(&fValueHasBeenComputedState, 2); > break; > case 1: > > while(InterlockedExchangeAdd(&fValueHasBeenComputedState,(LONG)0) > == (LONG)1) { } > // fall through > default: > // nothing to do > ; > } > } This is a disaster on a machine with more than 2 CPUs or hyper-threading. In the more than 2 CPUs case, two CPUs in the 'while' loop will totally saturate the FSB, causing the CPU that needs to release the lock to be unable to do so. In the hyper-threaded case, the virtual CPU in the while loop will steal resources from the thread that's doing useful work. DS .