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.

Atomic Compare and Exchange<br />

There are two versions: strong and weak.<br />

The weak may fail (and let you know) and must <strong>the</strong>refore be used <strong>in</strong> a<br />

loop.<br />

The functions compare <strong>the</strong> value at <strong>the</strong> location po<strong>in</strong>ted to by ptr<br />

with an expected value and if <strong>the</strong>y are equal writes a <strong>new</strong> value.<br />

The result of <strong>the</strong> comparison is returned.<br />

If <strong>the</strong> values are not equal, <strong>the</strong> current value is copied to expected, or<br />

actually to where <strong>the</strong> po<strong>in</strong>ter expected po<strong>in</strong>ts.<br />

The strong functions behave as:<br />

if (*ptr == *expected)<br />

*ptr = value;<br />

else<br />

*expected = *ptr;<br />

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

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

Saved successfully!

Ooh no, something went wrong!