In a binary tree, I'm trying to atomically replace left child of parent to a new node.
In the below method, pnode.left
is pointing to node
and I'm trying to change it
to replaceNode
.
In line1, childPtr
is pointing to pnode.left
In line2, oldChildPtr
is pointing to pnode.left
In line3, childPtr
is atomically changed from pointing topnode.left
to replaceNode
.
But pnode.left
is unchanged. I understand that this is how things work in java.
But how do I modify this code so that pnode.left
is atomically replaced with replaceNode
.
atomicReplaceLeftChild(node,pnode,replaceNode)
{
AtomicReference<Node> childPtr = new AtomicReference<Node>(pnode.left);
Node oldChildPtr = childPtr.get();
childPtr.compareAndSet(oldChildPtr, replaceNode);
}