<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi,<br>
      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).</p>
    <p>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.<br>
    </p>
    <p>Hope this helps.</p>
    <p>Maurizio<br>
    </p>
    <div class="moz-cite-prefix">On 27/03/2023 08:48, 刘希晨 wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CABUmArSBVZoe=Dm-Ne33rtgqkjM04i_mEbqTBP9GKcVukcDMkA@mail.gmail.com">
      
      <div dir="ltr">
        <div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
          have a question about using MemorySegment with
          Arena.openShared().</div>
        <div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
          don't know if the memory created by Arena.openShared() or any
          other shared scope would be safely accessed by multiple
          threads.</div>
        <div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">Simply
          put, if a writer thread wrote something into the MemorySegment
          and then passing it to the reader thread using a
          BlockingQueue,</div>
        <div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
          am confused if the writer operation would already be visible
          to the reader thread.</div>
        <div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
          have tested its visibility using jcstress and it turns out
          every Varhandle.get() will successfully read the data changed
          in another thread, </div>
        <div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">even
          without volatile or locks, so I am confused if the jvm has
          already created memory fence for memorysegment's access.</div>
        <div style="color:rgb(0,0,0);font-family:Arial;font-size:14px;margin:0px">I
          hope the authorities could offer a suggested way of accessing
          same memory segment in multiple threads in Panama FFI's
          documentation.</div>
      </div>
    </blockquote>
  </body>
</html>