How to execute atomic 64b read on x86 (Pentium and above)?

I would like to perform atomic reading of 64-bit 64b data on the x86 platform (guaranteed by Pentium or higher).

Is there any way to do this? (And no, I don’t want to use a critical section or a mutex for this, I want it to be locked).

+3
source share
3 answers

Use lock operations, here is a sample code:

LONGLONG AtomicRead(LONGLONG* p)
{
    return InterlockedCompareExchange64(p, 0, 0);
}

This compares the exchange with zero and sets p to zero if it is already zero - this is noop. InterlockedCompareExchange returns the original 64-bit value pointed to by p.

+4
source

, . lock cmpxchg8b.

+6

Use the lock functions * ().

There is no reading per se, but you can create Add () where you add 0.

0
source

Source: https://habr.com/ru/post/1707083/


All Articles