Question

I would like to perform and atomic read of 64b aligned 64b data on x86 platform (Pentium or above guaranteed).

Is there a way to do this? (And no, I do not want to use a critical section or a mutex for this, I want this to be lock-free).

Was it helpful?

Solution

Use the Interlocked operations, here's some sample code:

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

This does the compare exchange against zero and sets p to zero if it's already equal to zero -ie, it's a noop. InterlockedCompareExchange returns the original 64 bit value pointed to by p.

OTHER TIPS

This page describes how to do it. Basically you just have to use lock cmpxchg8b.

Use the Interlocked*() functions.

There's no read per se - but you can issue an Add() where you add 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top