summaryrefslogtreecommitdiff
path: root/devdocs/c/atomic%2Fatomic_init.html
blob: a6790747a9b0d1f6e198860bb468332d06a50b38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    <h1 id="firstHeading" class="firstHeading">atomic_init</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdatomic.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c11"> <td> <pre data-language="c">void atomic_init( volatile A* obj, C desired );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c11">(since C11)</span> </td> </tr>  </table> <p>Initializes the default-constructed atomic object <code>obj</code> with the value <code>desired</code>. The function is not atomic: concurrent access from another thread, even through an atomic operation, is a data race.</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_init)(...)</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 an atomic object to initialize </td>
</tr> <tr class="t-par"> <td> desired </td> <td> - </td> <td> the value to initialize atomic object with </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>(none)</p>
<h3 id="Notes"> Notes</h3> <p><code>atomic_init</code> is the only way to initialize dynamically-allocated atomic objects. For example:</p>
<div class="c source-c"><pre data-language="c">_Atomic int *p = malloc(sizeof(_Atomic int));
atomic_init(p, 42);</pre></div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.17.2.2 The atomic_init generic function (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.17.2.2 The atomic_init generic function (p: 201) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.17.2.2 The atomic_init generic function (p: 274-275) </li></ul>
</ul>           <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="atomic_var_init" title="c/atomic/ATOMIC VAR INIT"> <span class="t-lines"><span>ATOMIC_VAR_INIT</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c11">(C11)</span><span class="t-mark-rev t-deprecated-c17">(deprecated in C17)</span><span class="t-mark-rev t-until-c23">(removed in C23)</span></span></span></div> </td> <td> initializes a new atomic object <br> <span class="t-mark">(function macro)</span>  </td>
</tr> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/atomic/atomic_init" title="cpp/atomic/atomic init">C++ documentation</a></span> for <code>atomic_init</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; 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_init" class="_attribution-link">https://en.cppreference.com/w/c/atomic/atomic_init</a>
  </p>
</div>