Files
hustoj/web/cppreference-zh/chmhelp/cpp-atomic-atomic_ref-compare_exchange.html
2025-08-01 12:57:54 +08:00

116 lines
12 KiB
HTML

<!DOCTYPE html>
<html lang="zh" dir="ltr" class="client-nojs" xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh">
<head><meta http-equiv="x-ua-compatible" content="ie=edge">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>std::atomic_ref&lt;T&gt;::compare_exchange_weak, std::atomic_ref&lt;T&gt;::compare_exchange_strong</title>
<link rel="stylesheet" href="ext.css" />
<link rel="stylesheet" href="site_modules.css" />
</head>
<body class="mediawiki ltr sitedir-ltr ns-0 ns-subject page-cpp_atomic_atomic_ref_compare_exchange skin-cppreference2 action-view cpp-navbar">
<div id="cpp-content-base">
<div id="content"><a id="top"></a>
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">std::atomic_ref&lt;T&gt;::</span>compare_exchange_weak, <span style="font-size:0.7em; line-height:130%">std::atomic_ref&lt;T&gt;::</span>compare_exchange_strong</h1>
<div id="bodyContent">
<div id="contentSub"><span class="subpages">&lt; <a href="cpp.html">cpp</a>&lrm; | <a href="cpp-atomic.html">atomic</a>&lrm; | <a href="cpp-atomic-atomic_ref.html">atomic ref</a></span></div>
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
<table class="t-dcl-begin">
<tbody>
<tr class="t-dcl t-since-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span> compare_exchange_weak<span class="br0">(</span> T<span class="sy3">&amp;</span> expected, T desired,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2092">std::<span class="me2">memory_order</span></span></a> success,<br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2092">std::<span class="me2">memory_order</span></span></a> failure <span class="br0">)</span> <span class="kw4">const</span> <span class="kw1">noexcept</span><span class="sy4">;</span></div>
</td>
<td>(1)</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span> compare_exchange_weak<span class="br0">(</span> T<span class="sy3">&amp;</span> expected, T desired,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2092">std::<span class="me2">memory_order</span></span></a> order <span class="sy1">=</span><br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2098">std::<span class="me2">memory_order_seq_cst</span></span></a> <span class="br0">)</span> <span class="kw4">const</span> <span class="kw1">noexcept</span><span class="sy4">;</span></div>
</td>
<td>(2)</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span> compare_exchange_strong<span class="br0">(</span> T<span class="sy3">&amp;</span> expected, T desired,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2092">std::<span class="me2">memory_order</span></span></a> success,<br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2092">std::<span class="me2">memory_order</span></span></a> failure <span class="br0">)</span> <span class="kw4">const</span> <span class="kw1">noexcept</span><span class="sy4">;</span></div>
</td>
<td>(3)</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 &#36215;)</span></td>
</tr>
<tr class="t-dcl t-since-cxx20">
<td>
<div><span class="mw-geshi cpp source-cpp"><span class="kw4">bool</span> compare_exchange_strong<span class="br0">(</span> T<span class="sy3">&amp;</span> expected, T desired,<br /></span>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2092">std::<span class="me2">memory_order</span></span></a> order <span class="sy1">=</span><br /></p>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="cpp-atomic-memory_order.html"><span class="kw2098">std::<span class="me2">memory_order_seq_cst</span></span></a> <span class="br0">)</span> <span class="kw4">const</span> <span class="kw1">noexcept</span><span class="sy4">;</span></div>
</td>
<td>(4)</td>
<td><span class="t-mark-rev t-since-cxx20">(C++20 &#36215;)</span></td>
</tr>
</tbody>
<tbody>
<tr class="t-dcl-sep">
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
<p>&#21407;&#23376;&#22320;&#27604;&#36739;&#34987;&#24341;&#29992;&#23545;&#35937;&#30340;<a href="cpp-language-object.html">&#20540;&#34920;&#31034;</a>&#19982; <code>expected</code> &#30340;&#20540;&#34920;&#31034;&#65292;&#32780;&#33509;&#23427;&#20204;&#36880;&#20301;&#30456;&#31561;&#65292;&#21017;&#20197; <code>desired</code> &#26367;&#25442;&#21069;&#32773;&#65288;&#36827;&#34892;&#35835;&#20462;&#25913;&#20889;&#25805;&#20316;&#65289;&#12290;&#21542;&#21017;&#65292;&#21152;&#36733;&#23384;&#20648;&#20110;&#34987;&#24341;&#29992;&#23545;&#35937;&#30340;&#23454;&#38469;&#20540;&#21040; <code>expected</code> &#20013;&#65288;&#36827;&#34892;&#21152;&#36733;&#25805;&#20316;&#65289;&#12290;</p>
<p>&#35835;&#20462;&#25913;&#20889;&#21644;&#21152;&#36733;&#25805;&#20316;&#30340;&#20869;&#23384;&#27169;&#22411;&#20998;&#21035;&#26159; <code>success</code> &#21644; <code>failure</code> &#12290;&#22312; (2) &#21644; (4) &#29256;&#26412;&#20013;&#65292; <code>order</code> &#21516;&#26102;&#29992;&#20110;&#35835;&#20462;&#25913;&#20889;&#25805;&#20316;&#21644;&#21152;&#36733;&#25805;&#20316;&#65292;&#38500;&#20102;&#33509; <span class="t-c"><span class="mw-geshi cpp source-cpp">order <span class="sy1">==</span> <a href="cpp-atomic-memory_order.html"><span class="kw2097">std::<span class="me2">memory_order_acq_rel</span></span></a></span></span> &#25110; <span class="t-c"><span class="mw-geshi cpp source-cpp">order <span class="sy1">==</span> <a href="cpp-atomic-memory_order.html"><span class="kw2096">std::<span class="me2">memory_order_release</span></span></a></span></span> &#65292;&#21017;&#20998;&#21035;&#23558; <span class="t-lc"><a href="cpp-atomic-memory_order.html">std::memory_order_acquire</a></span> &#21644; <span class="t-lc"><a href="cpp-atomic-memory_order.html">std::memory_order_relaxed</a></span> &#29992;&#20110;&#21152;&#36733;&#25805;&#20316;&#12290;</p>
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">&#21442;&#25968;</span></h3>
<table class="t-par-begin">
<tr class="t-par">
<td>expected</td>
<td>-</td>
<td>&#21040;&#26399;&#24453;&#22312; <code>atomic_ref</code> &#25152;&#24341;&#29992;&#23545;&#35937;&#20013;&#25214;&#21040;&#30340;&#20540;&#30340;&#24341;&#29992;</td>
</tr>
<tr class="t-par">
<td>desired</td>
<td>-</td>
<td>&#33509;&#28385;&#36275;&#26399;&#24453;&#21017;&#35201;&#23384;&#20648;&#20110;&#34987;&#24341;&#29992;&#23545;&#35937;&#20013;&#30340;&#20540;</td>
</tr>
<tr class="t-par">
<td>success</td>
<td>-</td>
<td>&#33509;&#27604;&#36739;&#25104;&#21151;&#21017;&#29992;&#20110;&#35835;&#20462;&#25913;&#20889;&#25805;&#20316;&#30340;&#20869;&#23384;&#21516;&#27493;&#39034;&#24207;&#12290;&#23481;&#35768;&#25152;&#26377;&#20540;&#12290;</td>
</tr>
<tr class="t-par">
<td>failure</td>
<td>-</td>
<td>&#33509;&#27604;&#36739;&#22833;&#36133;&#21017;&#29992;&#20110;&#21152;&#36733;&#25805;&#20316;&#30340;&#20869;&#23384;&#21516;&#27493;&#39034;&#24207;&#12290;&#19981;&#33021;&#20026; <span class="t-lc"><a href="cpp-atomic-memory_order.html">std::memory_order_release</a></span> &#25110; <span class="t-lc"><a href="cpp-atomic-memory_order.html">std::memory_order_acq_rel</a></span></td>
</tr>
<tr class="t-par">
<td>order</td>
<td>-</td>
<td>&#20004;&#20010;&#25805;&#20316;&#30340;&#20869;&#23384;&#21516;&#27493;&#39034;&#24207;</td>
</tr>
</table>
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">&#36820;&#22238;&#20540;</span></h3>
<p>&#33509;&#25104;&#21151;&#26356;&#25913;&#34987;&#24341;&#29992;&#25968;&#25454;&#21017;&#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">true</span></span></span> &#65292;&#21542;&#21017;&#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">false</span></span></span> &#12290;</p>
<h3><span class="mw-headline" id=".E6.B3.A8.E8.A7.A3">&#27880;&#35299;</span></h3>
<p>&#27604;&#36739;&#21644;&#22797;&#21046;&#26159;&#36880;&#20301;&#30340;&#65288;&#31867;&#20284; <span class="t-lc"><a href="cpp-string-byte-memcmp.html">std::memcmp</a></span> &#21644; <span class="t-lc"><a href="cpp-string-byte-memcpy.html">std::memcpy</a></span> &#65289;&#65307;&#19981;&#20351;&#29992;&#26500;&#36896;&#20989;&#25968;&#12289;&#36171;&#20540;&#36816;&#31639;&#31526;&#25110;&#27604;&#36739;&#36816;&#31639;&#31526;&#12290;</p>
<p>&#20801;&#35768;&#24369;&#24418;&#24335; (1-2) &#34394;&#20551;&#22320;&#22833;&#36133;&#65292;&#21363;&#34920;&#29616;&#20026; <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="sy2">*</span>this <span class="sy3">!</span><span class="sy1">=</span> expected</span></span> &#65292;&#21363;&#20351;&#23427;&#20204;&#30456;&#31561;&#12290;&#27604;&#36739;&#24182;&#20132;&#25442;&#22312;&#24490;&#29615;&#20013;&#26102;&#65292;&#24369;&#29256;&#26412;&#22312;&#26576;&#20123;&#24179;&#21488;&#19978;&#23558;&#20135;&#29983;&#36739;&#22909;&#30340;&#24615;&#33021;&#12290;</p>
<p>&#24369;&#27604;&#36739;&#24182;&#20132;&#25442;&#20250;&#35201;&#27714;&#24490;&#29615;&#65292;&#32780;&#24378;&#29256;&#26412;&#19981;&#20250;&#35201;&#27714;&#26102;&#65292;&#26368;&#22909;&#29992;&#24378;&#29256;&#26412;&#65292;&#38500;&#38750; <code>T</code> &#30340;&#23545;&#35937;&#34920;&#31034;&#21487;&#33021;&#21253;&#21547;&#38519;&#38449;&#20301;&#65292;&#25110;&#30456;&#21516;&#20540;&#26377;&#22810;&#31181;&#23545;&#35937;&#34920;&#31034;&#65288;&#20363;&#22914;&#28014;&#28857; NaN &#65289;&#12290;&#37027;&#20123;&#24773;&#20917;&#19979;&#65292;&#24369;&#27604;&#36739;&#24182;&#20132;&#25442;&#36890;&#24120;&#33021;&#29992;&#65292;&#22240;&#20026;&#23427;&#22312;&#26576;&#20123;&#31283;&#23450;&#30340;&#23545;&#35937;&#34920;&#31034;&#19978;&#24555;&#36895;&#25910;&#25947;&#12290;</p>
<p>&#33509;&#26377;&#21442;&#19982; union &#26576;&#20123;&#25104;&#21592;&#65292;&#20294;&#38750;&#20854;&#20182;&#25104;&#21592;&#30340;&#20540;&#34920;&#31034;&#30340;&#20301;&#65292;&#21017;&#27604;&#36739;&#24182;&#20132;&#25442;&#21487;&#33021;&#22987;&#32456;&#22833;&#36133;&#65292;&#22240;&#20026;&#36825;&#31181;&#22635;&#20805;&#20301;&#22312;&#19981;&#21442;&#19982;&#27963;&#36291;&#25104;&#21592;&#30340;&#20540;&#34920;&#31034;&#26102;&#25317;&#26377;&#19981;&#30830;&#23450;&#20540;&#12290;</p>
<p>&#24573;&#30053;&#20915;&#19981;&#21442;&#19982;&#23545;&#35937;&#20540;&#34920;&#31034;&#30340;&#22635;&#20805;&#20301;&#12290;</p>
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">&#31034;&#20363;</span></h3>
<table class="metadata plainlinks ambox mbox-small-left ambox-notice" style="">
<tr>
<td class="mbox-empty-cell"></td>
<td class="mbox-text" style="">&#26412;&#33410;&#26410;&#23436;&#25104;<br />
&#21407;&#22240;&#65306;&#26242;&#26080;&#31034;&#20363;</td>
</tr>
</table>
</div>
<div class="visualClear"></div>
</div>
</div>
</div>
</body>
</html>