let's say we have a piece of very simple code:
if (Interlocked.Exchange(ref m_ResourceInUse, 1) == 0)
return;
Interlocked.Exchange ensures that one thread changes m_resourceInUse from 0 to 1 and sees
that m_resourceInUse was 0. This thread then returns.The other thread will change m_resourceInUse from a 1 to a 1. This thread will see that it did not change m_resourceInUse from a 0 ...
But "change m_resourceInUse from a 1 to a 1" is really unnecessary in hard ware level, CPU can ignore the instruction and do nothing, save some cycles that involves update register, main memory etc. So does Interlocked.Exchange changes m_resourceInUse from a 1 to a 1 or there is a special CPU instruction that can be mapped to not updating value if the condition is met?