c++xcodeatomic-swap

Declaration of Volatile Pointer Array in C++


I have a struct called Ambigous, and inside the struct I have an array of pointers to other Ambigous.

I want to use OSAtomic.h library, to do CompareandSwaps.

However I am having trouble getting the array to play nice.

 OSAtomicCompareAndSwapPtrBarrier(void *__oldValue, void *__newValue, 
void *volatile *__theValue)

is the compare and swap function.

and inside my struct I have

 Ambigous * volatile* list;

and the call is

bool res=OSAtomicCompareAndSwapPtrBarrier(current_node, new_node, local->list[pos]);

When I try to cas by

bool res=OSAtomicCompareAndSwapPtrBarrier(current_node, new_node, (void * volatile *)local->list[pos]);

I get a bad EXE_BAD_ACCESS

So i guess what i am answering is how should i declare the array of volatile pointers?


Solution

  • Perhaps you want

    bool res = OSAtomicCompareAndSwapPtrBarrier(
                  current_node, new_node, local->list + pos);
    

    Note that the CAS operation is basically

    bool CAS(void* old, void* new_, void* volatile* val) {
       /*atomic*/ {
         if (old == *val) {
             *val = new_;
             return true;
         } else
             return false;
       }
    }
    

    If you pass a list[pos], the 3rd argument will be of type Ambigous*, and *val will be of type struct Ambigous which cannot be compared to a pointer.