Subj : Re: Optimization to Jeffrey Richter's COptex (Windows)? To : comp.programming.threads From : Alexander Terekhov Date : Thu Jan 27 2005 02:41 am Joseph Seigh wrote: [...] > BOOL CoptexTryEnter(coptex_t *lock) { > > CoptexSpin(lock); > if (InterlockedCompareExchange(&(lock->m_lLockCount), 1, 0) == 0) { > lock->m_dwThreadId = GetCurrentThreadId(); > lock->m_lRecurseCount = 1; > return TRUE; > } > > else if (lock->m_dwThreadId == GetCurrentThreadId()) { > lock->m_lRecurseCount++; > return TRUE; > } > > return FALSE; > } > > I'm assuming GetCurrentThreadId or an equivalent intrinsic is very > efficient. With an unconditional increment of the lockcount you > end up with the threadid final check after a memory barrier anyway > so paranoids like Alexander should be happy. :) Paranoids like me know that according to the MS docu, InterlockedCompareExchange() performs nothing ("no operation") if the Destination value is NOT equal to the Comperand value. I can imagine reasonable implementation for XBOX Next that would have neither trailing "isync" (for acquire msync) nor leading "sync" (for release msync) instructions on that path. regards, alexander. .