diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/atomic%2Fatomic_compare_exchange.html | |
new repository
Diffstat (limited to 'devdocs/c/atomic%2Fatomic_compare_exchange.html')
| -rw-r--r-- | devdocs/c/atomic%2Fatomic_compare_exchange.html | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/devdocs/c/atomic%2Fatomic_compare_exchange.html b/devdocs/c/atomic%2Fatomic_compare_exchange.html new file mode 100644 index 00000000..5075a508 --- /dev/null +++ b/devdocs/c/atomic%2Fatomic_compare_exchange.html @@ -0,0 +1,45 @@ + <h1 id="firstHeading" class="firstHeading">atomic_compare_exchange_weak, atomic_compare_exchange_strong, atomic_compare_exchange_weak_explicit, atomic_compare_exchange_strong_explicit</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdatomic.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">_Bool atomic_compare_exchange_strong( volatile A* obj, + C* expected, C desired );</pre> +</td> <td> (1) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">_Bool atomic_compare_exchange_weak( volatile A *obj, + C* expected, C desired );</pre> +</td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">_Bool atomic_compare_exchange_strong_explicit( volatile A* obj, + C* expected, C desired, + memory_order succ, + memory_order fail );</pre> +</td> <td> (3) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">_Bool atomic_compare_exchange_weak_explicit( volatile A *obj, + C* expected, C desired, + memory_order succ, + memory_order fail );</pre> +</td> <td> (4) </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr> </table> <p>Atomically compares the contents of memory pointed to by <code>obj</code> with the contents of memory pointed to by <code>expected</code>, and if those are bitwise equal, replaces the former with <code>desired</code> (performs read-modify-write operation). Otherwise, loads the actual contents of memory pointed to by <code>obj</code> into <code>*expected</code> (performs load operation).</p> +<p>The memory models for the read-modify-write and load operations are <code>succ</code> and <code>fail</code> respectively. The (1-2) versions use <code><a href="memory_order" title="c/atomic/memory order">memory_order_seq_cst</a></code> by default.</p> +<p>The weak forms ((2) and (4)) of the functions are allowed to fail spuriously, that is, act as if <code>*obj != *expected</code> even if they are equal. When a compare-and-exchange is in a loop, the weak version will yield better performance on some platforms. When a weak compare-and-exchange would require a loop and a strong one would not, the strong one is preferable.</p> +<p>This is a <a href="../language/generic" title="c/language/generic">generic function</a> defined for all <a href="../language/atomic" title="c/language/atomic">atomic object types</a> <code>A</code>. The argument is pointer to a volatile atomic type to accept addresses of both non-volatile and <a href="../language/volatile" title="c/language/volatile">volatile</a> (e.g. memory-mapped I/O) atomic objects, and volatile semantic is preserved when applying this operation to volatile atomic objects. <code>C</code> is the non-atomic type corresponding to <code>A</code>.</p> +<p>It is unspecified whether the name of a generic function is a macro or an identifier declared with external linkage. If a macro definition is suppressed in order to access an actual function (e.g. parenthesized like <code>(atomic_compare_exchange)(...)</code>), or a program defines an external identifier with the name of a generic function, the behavior is undefined.</p> +<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> obj </td> <td> - </td> <td> pointer to the atomic object to test and modify </td> +</tr> <tr class="t-par"> <td> expected </td> <td> - </td> <td> pointer to the value expected to be found in the atomic object </td> +</tr> <tr class="t-par"> <td> desired </td> <td> - </td> <td> the value to store in the atomic object if it is as expected </td> +</tr> <tr class="t-par"> <td> succ </td> <td> - </td> <td> the memory synchronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted. </td> +</tr> <tr class="t-par"> <td> fail </td> <td> - </td> <td> the memory synchronization ordering for the load operation if the comparison fails. Cannot be <code><a href="memory_order" title="c/atomic/memory order">memory_order_release</a></code> or <code><a href="memory_order" title="c/atomic/memory order">memory_order_acq_rel</a></code> and cannot specify stronger ordering than <code>succ</code> </td> +</tr> +</table> <h3 id="Return_value"> Return value</h3> <p>The result of the comparison: <code>true</code> if <code>*obj</code> was equal to <code>*exp</code>, <code>false</code> otherwise.</p> +<h3 id="Notes"> Notes</h3> <p>The behavior of <code>atomic_compare_exchange_*</code> family is as if the following was executed atomically:</p> +<div class="c source-c"><pre data-language="c">if (memcmp(obj, expected, sizeof *obj) == 0) { + memcpy(obj, &desired, sizeof *obj); + return true; +} else { + memcpy(expected, obj, sizeof *obj); + return false; +}</pre></div> <h3 id="References"> References</h3> <ul> +<li> C17 standard (ISO/IEC 9899:2018): </li> +<ul><li> 7.17.7.4 The atomic_compare_exchange generic functions (p: 207) </li></ul> +<li> C11 standard (ISO/IEC 9899:2011): </li> +<ul><li> 7.17.7.4 The atomic_compare_exchange generic functions (p: 283-284) </li></ul> +</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="atomic_exchange" title="c/atomic/atomic exchange"> <span class="t-lines"><span>atomic_exchange</span><span>atomic_exchange_explicit</span></span></a></div> +<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span></span></span></div> </td> <td> swaps a value with the value of an atomic object <br> <span class="t-mark">(function)</span> </td> +</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/atomic/atomic_compare_exchange" title="cpp/atomic/atomic compare exchange">C++ documentation</a></span> for <code>atomic_compare_exchange_weak, atomic_compare_exchange_strong, atomic_compare_exchange_weak_explicit, atomic_compare_exchange_strong_explicit</code> </td> +</tr> </table> <div class="_attribution"> + <p class="_attribution-p"> + © cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br> + <a href="https://en.cppreference.com/w/c/atomic/atomic_compare_exchange" class="_attribution-link">https://en.cppreference.com/w/c/atomic/atomic_compare_exchange</a> + </p> +</div> |
