[concurrency-interest] volatile guarantee for directbuffer

Holger Peine Holger.Peine at fh-hannover.de
Tue Feb 14 10:53:53 EST 2012


Am 14.02.2012 15:09, schrieb Talip Ozturk:
> Say we have thread1 and thread2. Both can access a volatile boolean
> variable, 'volatileVar' and DirectByteBuffer instance 'directBuffer'
> 
> thread1:
> directBuffer.put(myByteArray);
> directBuffer.putLong(myLong);
> volatileVar = true;
> 
> thread2:
> if (volatileVar) {
>   // read the directBuffer
>   directBuffer.get(myByteArray);
>   long myLong = directBuffer.getLong();
> }
> 
> Can I assume that thread2 did read the byte[] and long values?

Yes.

> Does volatile-reads give us happens-before guarantee for OffHeap data?

I'm not sure what you mean be OffHeap, but actually that doesn't matter
at all: Writing to a volatile will make _all_ writes that happened
earlier in the same thread visible to all threads, no matter what
and where that earlier-written data is. (I use to tell my students
"writing to a volatile flushes the cache".)

Regards,
Holger Peine

-- 
Prof. Dr. Holger Peine
Hochschule Hannover, Fakultät IV, Abt. Informatik
Tel: +49(511)9296-1830  Fax: -1810 (shared, please state my name)
Ricklinger Stadtweg 120, D-30459 Hannover, Germany


More information about the Concurrency-interest mailing list