Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relaxed write, what does it mean?

private static class Node<E> {
    volatile E item;
    volatile Node<E> next;

    /**
     * Constructs a new node.  Uses relaxed write because item can
     * only be seen after publication via casNext.
     */
    Node(E item) {
        UNSAFE.putObject(this, itemOffset, item);
    }

It comes from java.util.concurrent.ConcurrentLinkedQueue.java

What does it mean, relaxed write?

like image 655
Gilgamesz Avatar asked Apr 27 '26 00:04

Gilgamesz


1 Answers

It is a good question. But the clue to understanding this term lies in ... JDK 9, where sun.misc.Unsafe has become (or will become - what is more appropriate to say, see here) public API.

In JDK 9 the corresponding place, which you refers to, is implemented as follows:

static final class Node<E> {
    volatile E item;
    volatile Node<E> next;

    /**
     * Constructs a node holding item.  Uses relaxed write because
     * item can only be seen after piggy-backing publication via CAS.
     */
    Node(E item) {
        ITEM.set(this, item);
    }
    ...
}

Where ITEM is instance of VarHandle class which implements similar functionality as sun.misc.Unsafe. Now, we can look at this method's JavaDoc description and find the following:

Sets the value of a variable to the newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final. Commonly referred to as plain write access.

In other words, we can conclude, that relaxed write is the same as plain write access. In other words, I believe that Michael's commentary above is right:

... its like the opposite of volatile - a write that is not guaranteed to be seen across threads.

(see the opposite method setVolatile which works as if the variable was declared volatile).

like image 110
Andremoniy Avatar answered Apr 28 '26 12:04

Andremoniy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!