How do I atomically swap 2 ints in C#?

后端 未结 8 775
执念已碎
执念已碎 2020-11-29 06:50

What (if any) is the C# equivalent of the x86 asm xchg instruction?

With that command, which imo is a genuine exchange (unlike Interlocked.Exchange), I

8条回答
  •  孤独总比滥情好
    2020-11-29 07:03

    Outside of Interlocked.Exchange, I'm assuming the XCHG command is probably an implementation of the XOR Swap, so you could write your own.

    C syntax: (from wikipedia link)

     void xorSwap (int *x, int *y) {
         if (x != y) {
             *x ^= *y;
             *y ^= *x;
             *x ^= *y;
         }
     }
    

    Edit this is not atomic, you would have to synchronize it yourself

提交回复
热议问题