在作为JSR166的一部分引入的类中,作者使用所谓的填充来填充Striped64.Cell类的单个值字段。为什么jsr166e.Striped64.Cell类中值域的额外填充值?
这里是一个类的摘录:
/**
* Padded variant of AtomicLong supporting only raw accesses plus CAS. The value field is placed
* between pads, hoping that the JVM doesn't reorder them.
* <p/>
* JVM intrinsics note: It would be possible to use a release-only form of CAS here, if it were
* provided.
*/
static final class Cell {
volatile long p0, p1, p2, p3, p4, p5, p6;
volatile long value;
volatile long q0, q1, q2, q3, q4, q5, q6;
...
笔者然后使用CAS原子地修改值。
在Striped64类中,作者还使用不安全来访问两个其他字段,但不应用任何此类填充。
我的问题是:为什么需要做这样的事情,引入14个冗余字段填充单个值字段?
感谢您的回答。我认为这是正确的,但为什么额外的q0 ... q6作为p0..p6 +值的总体大小已经是64字节,这在我的理解中完全适合单个缓存行?是否容纳实现更长缓存行的其他体系结构? – silkentrance
@silkentrance它将使用2个缓存行。如果你只有p0,p1,p6的值,那么你最终可能会在一个高速缓存行上使用p0-p6,其他数据和另一行上的值以及其他数据。 – assylias