Created
February 9, 2011 18:06
-
-
Save viktorklang/818918 to your computer and use it in GitHub Desktop.
Optimistic concurrency using AtomicReference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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