Question

I have a tree node structure as:

struct node
{
    unsigned long key;                                           
    tbb::atomic<struct node*> lChild;
    tbb::atomic<struct node*> rChild;
};

I would be doing compare_and_swap on lChild and rChild. I want make the left and right child as array elements and still be able to do CAS on individual array elements.

Note: I do not intend to do a double CAS

I tried this:

struct node
{
    unsigned long key;
    tbb::atomic<struct node*> childrenArray[2];
};

and this:

struct node
{
    unsigned long key;                                           
    tbb::atomic<struct node**> childrenArray;
};

but here the individual array elements are not atomic. How do I modify this structure so that I can do a CAS like:

node->childrenArray[0].compare_and_swap(newNode,oldNode);
Was it helpful?

Solution

Why the first approach doesn't work for you? Unless you intend to perform atomic update on the whole array, it is the way to go. I compiled the following code successfully:

struct node
{
    unsigned long key;
    tbb::atomic<struct node*> childrenArray[2];
};

void main() {
    node n;
    n.childrenArray[0].compare_and_swap(0,0);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top