29.11.2015 Views

The C11 and C++11 Concurrency Model

1ln7yvB

1ln7yvB

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

135<br />

int main() {<br />

atomic_int x = 0;<br />

atomic_int y = 0;<br />

int a1,a2;<br />

{{{ { a1 = x.load(relaxed);<br />

y.store(a1,relaxed); }<br />

||| { a2 = y.load(relaxed);<br />

x.store(a2,relaxed); }<br />

}}}<br />

return 0;<br />

}<br />

c:R RLX x=42 g:R RLX y=42<br />

sb sb<br />

rf rf<br />

f:W RLX y=42 j:W RLX x=42<br />

Forbidden by x86, Power <strong>and</strong> ARM architectures. C/<strong>C++11</strong> should forbid this.<br />

As the formal model st<strong>and</strong>s, any single value may be read by the two atomic reads.<br />

<strong>The</strong> designers of the memory model intend this execution of the program to be forbidden,<br />

<strong>and</strong> the st<strong>and</strong>ard imposes a restriction in 29.3p9 that is intended to forbid this sort of<br />

execution. In fact, in 29.3p10 the st<strong>and</strong>ard says explicitly that 29.3p9 forbids it:<br />

29.3p10<br />

[...]<br />

// Thread 1:<br />

r1 = y.load(memory order relaxed);<br />

x.store(r1, memory order relaxed);<br />

// Thread 2:<br />

r2 = x.load(memory order relaxed);<br />

y.store(r2, memory order relaxed);<br />

[29.3p9 implies that this] may not produce r1 = r2 = 42 [...]<br />

Thisexecution oftheprogram violates programmer intuition: thefact that thewritten<br />

value is dependent on the read value implies a causal relationship, as do the reads-from<br />

edges. Together these edges form a cycle, <strong>and</strong> that cycle is what seems to violate intuition<br />

<strong>and</strong> separate this from more benign executions. Analogous programs on x86, Power <strong>and</strong><br />

ARM processors would all forbid this execution, <strong>and</strong> the st<strong>and</strong>ard explicitly forbids this<br />

execution. Moreover, allowing executions of this sort for some types, pointers for example,<br />

would allow us to write programs that violate run-time invariants by, say, forging pointers<br />

— see Section 5.10.3 for a discussion of Java’s safety guarantees in the context of the thin-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!