Matej Tymes
Matej Tymes

Reputation: 1724

Cache flush on CyclicBarrier or CountDownLatch like when using synchronized keyword

Is there some way how to ensure that java flushes the cache of writes that have been done before the CyclicBarrier or CountDownLatch allows us to continue (as the synchronized keyword does) without using the synchronized keyword?

Upvotes: 2

Views: 469

Answers (1)

irreputable
irreputable

Reputation: 45443

I think it is already guaranteed by the API.

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/CyclicBarrier.html

Memory consistency effects: Actions in a thread prior to calling await() happen-before actions that are part of the barrier action, which in turn happen-before actions following a successful return from the corresponding await() in other threads.

http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html#MemoryVisibility

The results of a write by one thread are guaranteed to be visible to a read by another thread only if the write operation happens-before the read operation. ... Actions prior to calling CyclicBarrier.await happen-before actions performed by the barrier action, and actions performed by the barrier action happen-before actions subsequent to a successful return from the corresponding await in other threads.


This means

thread 1                  thread 2

write x1;                 write x2
barrier.await();          barrier.await();
read x2                   read x1

no additional synchronization is needed; read x2 will see the result of write x2

Upvotes: 5

Related Questions