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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Happens Before and Data Races<br />

We will look at <strong>the</strong> details of <strong>the</strong> happens before relation and what it<br />

means <strong>in</strong> terms of execut<strong>in</strong>g C code at <strong>the</strong> mach<strong>in</strong>e <strong>in</strong>struction level<br />

below, after <strong>in</strong>troduc<strong>in</strong>g happens before <strong>in</strong>formally.<br />

It is <strong>the</strong> unlock/lock pair which guarantees that <strong>the</strong> write happens<br />

before <strong>the</strong> read and that <strong>the</strong> data becomes visible to <strong>the</strong> o<strong>the</strong>r thread.<br />

S<strong>in</strong>ce <strong>the</strong>re is no synchronization <strong>in</strong> <strong>the</strong> previous slide <strong>the</strong>re is a data<br />

race, obviously.<br />

An unlock on m synchronizes with a lock on m.<br />

Recall that mutex unlock/lock is one of three ma<strong>in</strong> k<strong>in</strong>ds of<br />

synchronizations which orders two variable accesses.<br />

The o<strong>the</strong>r two are:<br />

Release on an atomic object M followed by acquire or consume on M<br />

Memory fences.<br />

There are additional important details for atomic objects and fences<br />

which we will see later.<br />

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

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

Saved successfully!

Ooh no, something went wrong!