Skip to content

Instantly share code, notes, and snippets.

@msullivan
Created December 15, 2014 20:12
Show Gist options
  • Select an option

  • Save msullivan/6370cf65d7e4b651e2ac to your computer and use it in GitHub Desktop.

Select an option

Save msullivan/6370cf65d7e4b651e2ac to your computer and use it in GitHub Desktop.
Thin-air-read bad case
// Out of thin air writes are really scary.
// Example adapted from http://dl.acm.org/citation.cfm?id=2618134 to
// work under http://svr-pes20-cppmem.cl.cam.ac.uk/cppmem/index.html
// Can wind up with an = &b, bn = &a!
int main() {
int *p1, *p2;
atomic_int a, an, b, bn;
a = &an;
b = &bn;
{{{ { p1 = a.load(memory_order_relaxed);
atomic_store_explicit(*p1, &a, memory_order_relaxed); }
||| { p2 = b.load(memory_order_relaxed);
atomic_store_explicit(*p2, &b, memory_order_relaxed); } }}};
return 0;
}
// But if there are no atomics, everything is fine!
int main() {
int *p1, *p2;
int a, an, b, bn;
a = &an;
b = &bn;
{{{ { p1 = a;
*p1 = &a; }
||| { p2 = b;
*p2 = &b; } }}};
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment