Skip to content

Instantly share code, notes, and snippets.

@Blaisorblade
Last active December 23, 2015 00:09
Show Gist options
  • Save Blaisorblade/6551895 to your computer and use it in GitHub Desktop.
Save Blaisorblade/6551895 to your computer and use it in GitHub Desktop.
JMM-safe implementation of memory barriers
object JMMBarriers {
@volatile private var volatile: Int = 0
/** Enforce a write barrier.
*
* Writes before this will not be reordered after this barrier, and will be
* visible from any other thread which performs a read barrier.
*/
def writeBarrier() { volatile = 1 }
/** Enforce a read barrier.
*
* Reads after this will not be reordered before this barrier, and will see
* the effects of any writes from a thread which performed a write barrier
* before this one.
*/
def readBarrier() { volatile }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment