Skip to content

Instantly share code, notes, and snippets.

@viktorklang
Created February 9, 2011 18:06
Show Gist options
  • Save viktorklang/818918 to your computer and use it in GitHub Desktop.
Save viktorklang/818918 to your computer and use it in GitHub Desktop.
Optimistic concurrency using AtomicReference
class MiniSTM[T <: AnyRef](initialValue: T) {
private val value = new AtomicReference[T](initialValue)
def apply(fun: T => T) {
@tailrec def update(expect: T): Unit =
if ( !value.compareAndSet(expect, fun(expect)) ) update(value.get)
update(value.get)
}
def apply() = value.get
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment