The JLS section 17.7 says that plain long and double fields may observe
word tearing, but volatile long and double fields will not:
https://docs.oracle.com/javase/specs/jls/se20/html/jls-17.html#jls-17.7

How this guarantee is upheld is up to the JVM implementors, but on 64-bit
platforms it's usually trivial by just using load and store operations of
the correct bit-width.

Chris

On Fri, Jun 9, 2023 at 1:58 AM Fred Castaneras <[email protected]>
wrote:

> Hello,
>
>
> So I know the difference between getLong and getLongVolatile =>
> visibility. By using getLongVolatile I avoid any thread/process cache and I
> can be sure to read the latest contents from memory. Great! But how about
> synchronization / atomicity? *Do any of these two methods (getLong and
> getLongVolatile) will protect me from a writer in another process writing
> my 8 bytes while I'm reading them?*
>
>
> From practical experience using this, I would think so, but just wanted to
> run that question through you guys to see what you have to say about that.
> Perhaps only if the writer is also using putLong and putLongVolatile? What
> about if the writer is writing byte by byte with putByte or
> putByteVolatile? Would this situation cause a race-condition between reader
> and writer?
>
>
> And a bonus questions:
>
>
> How such synchronization / atomicity would be implemented at the C level
> by Unsafe? Is the C source code of Unsafe available somewhere so we can
> take a look to find out?
>
>
> -Fred
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "mechanical-sympathy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/mechanical-sympathy/0b8a3784-5df3-4648-9d3c-918ab4694564n%40googlegroups.com
> <https://groups.google.com/d/msgid/mechanical-sympathy/0b8a3784-5df3-4648-9d3c-918ab4694564n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web, visit 
https://groups.google.com/d/msgid/mechanical-sympathy/CAHXi_0dx%2BUaGvvTg%2BRGss2C4VhbgNXnpKKPLv79VQXzLRL0aLA%40mail.gmail.com.

Reply via email to