如何从地址中窃取 2 个 MSB 来执行原子操作?我正在尝试做一个单词 CAS
一个例子
public class Node
{
long key;
long value;
Node lchild; // format is flag1,flag2,address
Node rchild; // format is flag1,flag2,address
}
public void createNode()
{
Node n1 = new Node(); //this should create a node with format 0,0,address1
}
public void setFlag1(Node n1)
{
Now the new address should be in format 1,0,address1
}
public void setFlag2(Node n1)
{
Now the new address should be in format 0,1,address1
}
AtomicReference
如果我只需要一个额外的标志,可以使用。
AtomicStampedReference
可以使用,但效率不高,因为它创建了一个包含时间戳和参考的额外框。
C 语言中的一个类似问题在 从指针中窃取位时进行了讨论