lock issue RRS feed

  • Question

  • Has the same effect this two operations?

    1. Interlocked.Decrement(ref myCounter);
    2. lock(myLock) myCounter-=1;

    I need decrement a variable inside a 'lock', and i need know if Interlocked is needed to perform a atomic decrement.

    Thursday, October 11, 2007 1:17 PM


  • Same thing, using Interlocked will be a *lot* more efficient.  Depending on the CPU architecture, it will use a built-in CPU primitive to guarantee atomicity.  On 32-bit Intel/AMD multi-core CPUs, it will use the lock xadd instruction.
    Thursday, October 11, 2007 1:34 PM