If you can use Java 11, Take a look to the VarHandle API, using VarHandles is as fast as using Unsafe, but it does not crash.
For an explanation of the different order modes, [ https://gee.cs.oswego.edu/dl/html/j9mm.html | https://gee.cs.oswego.edu/dl/html/j9mm.html ] Bonus answer: as far as i know, VMs do not rely on C++ atomics but generates their own assembly codes (for the interpreter and for the JITs). regards, Rémi > From: "Fred Castaneras" <[email protected]> > To: "mechanical-sympathy" <[email protected]> > Sent: Friday, June 9, 2023 12:07:53 AM > Subject: Unsafe getLong/getLongVolatile has to read 8 bytes. It atomic / > synchronized? > 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 [ mailto:[email protected] | > [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?utm_medium=email&utm_source=footer > | > https://groups.google.com/d/msgid/mechanical-sympathy/0b8a3784-5df3-4648-9d3c-918ab4694564n%40googlegroups.com > ] . -- 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/1891937154.76839809.1686322028788.JavaMail.zimbra%40univ-eiffel.fr.
