06.08.2013 Views

Threads in the new ISO C Standard from 2011

Threads in the new ISO C Standard from 2011

Threads in the new ISO C Standard from 2011

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

An Example<br />

_Atomic <strong>in</strong>t m;<br />

<strong>in</strong>t u;<br />

// Thread 1 Thread 2<br />

u = 1;<br />

atomic_thread_fence(memory_order_release); // A<br />

atomic_store_explicit(&m, 1, memory_order_relaxed); // X<br />

atomic_load_explicit(&m, memory_order_relaxed); // Y<br />

atomic_thread_fence(memory_order_acquire); // B<br />

pr<strong>in</strong>tf("u = %d\n", u);<br />

Accesses to atomic objects do not produce data races.<br />

Simply runn<strong>in</strong>g <strong>the</strong> two threads does not order <strong>the</strong>m <strong>in</strong> any way.<br />

Only if Thread 2 reads m after <strong>the</strong> write <strong>the</strong>n it knows that <strong>the</strong> value<br />

of u is correct.<br />

The modification of u happens before <strong>the</strong> read of u.<br />

Jonas Skeppstedt (js@cs.lth.se) Lecture 6 2013 29 / 70

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

Saved successfully!

Ooh no, something went wrong!