Question about multi threads visiting shared arena
Paul Sandoz
paul.sandoz at oracle.com
Mon Mar 27 15:33:51 UTC 2023
It may be luck but it may also be because of the "happen-before” relationship that is specified by BlockIngQueue:
“
* <p>Memory consistency effects: As with other concurrent
* collections, actions in a thread prior to placing an object into a
* {@code BlockingQueue}
* <a href="package-summary.html#MemoryVisibility"><i>happen-before</i></a>
* actions subsequent to the access or removal of that element from
* the {@code BlockingQueue} in another thread.
“
This is a complex subject, and there is no specific recommended way of interacting concurrently with state, be it for a memory segment, an array, or an object. The platform provides a number of synchronizing primitives and concurrent collections, and those should be used in such cases.
So we could provide an api note that talks in general terms e.g. a segment whose memory is accessible by any thread is not thread-safe; in the absence of external synchronization, they do not support concurrent access to that memory by multiple threads.
Where "external synchronization” is doing a lot of heavy lifting :-)
Paul.
> On Mar 27, 2023, at 2:07 AM, Maurizio Cimadamore <Maurizio.Cimadamore at Oracle.COM> wrote:
>
> Hi,
> accessing a shared memory segment is pretty much the same as accessing a byte buffer, or a Java array instance. There are no additional memory fences that are added by our implementation when using "plain" access (as in all the other APIs I have listed), so, in the general case you would probably need to use VarHandle::fullFence() or something similar (to make sure all writes are flushed when one thread is done with the segment).
>
> It is surprising to see that it worked even w/o adding synchronization/fencing, but that might just be "luck", or the particular system you are using, as is often the case with these matters. From a low-level/implementation perspective there is absolutely no happens-before relationship inserted between writes on a memory segment and reads on the same segment (when using VarHandle::get - that is - a "plain" read/write). That is, plain access really means plain access.
>
> Hope this helps.
>
> Maurizio
>
> On 27/03/2023 08:48, 刘希晨 wrote:
>> I have a question about using MemorySegment with Arena.openShared().
>> I don't know if the memory created by Arena.openShared() or any other shared scope would be safely accessed by multiple threads.
>> Simply put, if a writer thread wrote something into the MemorySegment and then passing it to the reader thread using a BlockingQueue,
>> I am confused if the writer operation would already be visible to the reader thread.
>> I have tested its visibility using jcstress and it turns out every Varhandle.get() will successfully read the data changed in another thread,
>> even without volatile or locks, so I am confused if the jvm has already created memory fence for memorysegment's access.
>> I hope the authorities could offer a suggested way of accessing same memory segment in multiple threads in Panama FFI's documentation.
More information about the panama-dev
mailing list