Skip to content

Instantly share code, notes, and snippets.

@gaul
Last active December 16, 2015 19:39
Show Gist options
  • Save gaul/5486495 to your computer and use it in GitHub Desktop.
Save gaul/5486495 to your computer and use it in GitHub Desktop.
double checked locking alternative
enum { uninitialized, initializing, initialized };
static state = uninitialized;
static var = 0;
while (true) {
switch(cmpxchg(state, initializing, uninitialized)) {
case uninitialized:
var = stmt;
xchg(state, initialized);
return;
case initializing:
continue;
case initialized:
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment