Thread safety of the shared piece of memory - Java Memory Model

245 views
Skip to first unread message

r r

unread,
Jul 29, 2021, 11:47:52 AM7/29/21
to mechanical-sympathy
Hello,

We have the simple pseudocode:

void main(String[] args) {
    MemoryBuffer b = new MemoryBuffer(); // it is a off-heap native piece of memory allocated by malloc via JNI
    b.init(); // it just write some data to the memory buffer by JNI
    startThreadsThatReadsFromBuffer(b); // this functions starts threads that read the b
    // There is no locks / synchronization etc.
}

It seems to be thread-safe in the sense of Java Memory Model. But, I cannot convince myself why it is correct. Especially, how can I be sure that worker threads see completely intizalized buffer b?

Peter Veentjer

unread,
Jul 29, 2021, 2:30:46 PM7/29/21
to mechanica...@googlegroups.com
There is a happens before edge between calling the start method of a thread and the thread running.

On JMM level that is sufficient. 

--
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 mechanical-symp...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/mechanical-sympathy/84ab88bc-e0bf-4e19-8c39-eeea619bf0f0n%40googlegroups.com.

r r

unread,
Jul 29, 2021, 5:31:27 PM7/29/21
to mechanical-sympathy
Thanks
Reply all
Reply to author
Forward
0 new messages