117 lines
9.7 KiB
HTML
117 lines
9.7 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>C++ 具名要求:互斥体 (Mutex)</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_named_req_Mutex 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%">C++ 具名要求:</span><span style="font-style:italic">互斥体</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Mutex)</span></h1>
|
|
<div id="bodyContent">
|
|
<div id="contentSub"><span class="subpages">< <a href="cpp.html">cpp</a>‎ | <a href="cpp-named_req.html">named req</a></span></div>
|
|
<div id="mw-content-text" lang="zh" dir="ltr" class="mw-content-ltr" xml:lang="zh">
|
|
<p><strong class="selflink"><span style="font-style:italic">互斥体</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Mutex)</span></strong> 要求扩展<a href="cpp-named_req-Lockable.html"><span style="font-style:italic">可锁定</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Lockable)</span></a> 要求以包含线程间同步。</p>
|
|
<h3><span class="mw-headline" id=".E8.A6.81.E6.B1.82">要求</span></h3>
|
|
<ul>
|
|
<li><a href="cpp-named_req-Lockable.html"><span style="font-style:italic">可锁定</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Lockable)</span></a></li>
|
|
<li><a href="cpp-named_req-DefaultConstructible.html"><span style="font-style:italic">可默认构造</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(DefaultConstructible)</span></a></li>
|
|
<li><a href="cpp-named_req-Destructible.html"><span style="font-style:italic">可析构</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Destructible)</span></a></li>
|
|
<li>不可复制</li>
|
|
<li>不可移动</li>
|
|
</ul>
|
|
<p>对于互斥 (<span style="font-style:italic">互斥体</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Mutex)</span> ) 类型的对象 <code><b>m</b></code> :</p>
|
|
<ul>
|
|
<li>表达式 <span class="t-c"><span class="mw-geshi cpp source-cpp">m.<span class="me1">lock</span><span class="br0">(</span><span class="br0">)</span></span></span> 有下列属性</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>表现原子操作。</li>
|
|
<li>阻塞调用方线程,直到能获得互斥的排他性所有权为止。</li>
|
|
<li>先于同一互斥上的 <span class="t-c"><span class="mw-geshi cpp source-cpp">m.<span class="me1">unlock</span><span class="br0">(</span><span class="br0">)</span></span></span> 的操作<i>同步于</i>此锁操作(等价于释放获得 <span class="t-lc"><a href="cpp-atomic-memory_order.html">std::memory_order</a></span> )</li>
|
|
<li>若调用方线程已占有互斥,则行为未定义(除非 m 是 <span class="t-lc"><a href="cpp-thread-recursive_mutex.html">std::recursive_mutex</a></span> 或 <span class="t-lc"><a href="cpp-thread-recursive_timed_mutex.html">std::recursive_timed_mutex</a></span> )</li>
|
|
<li>错误时可能抛出 <span class="t-lc"><a href="cpp-error-system_error.html">std::system_error</a></span> 类型的异常,拥有下例错误码:</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>若调用方线程无要求的权限,则为 <span class="t-lc"><a href="cpp-error-errc.html">std::errc::operation_not_permitted</a></span></li>
|
|
<li>若实现检测出此线程将导致死锁,则为 <span class="t-lc"><a href="cpp-error-errc.html">std::errc::resource_deadlock_would_occur</a></span></li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
</dl>
|
|
<table class="t-rev-begin">
|
|
<tr class="t-rev t-until-cxx17">
|
|
<td>
|
|
<dl>
|
|
<dd>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>若互斥已被锁,则为 <span class="t-lc"><a href="cpp-error-errc.html">std::errc::device_or_resource_busy</a></span></li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
</dd>
|
|
</dl>
|
|
</td>
|
|
<td><span class="t-mark-rev t-until-cxx17">(C++17 前)</span></td>
|
|
</tr>
|
|
</table>
|
|
<ul>
|
|
<li>表达式 <span class="t-c"><span class="mw-geshi cpp source-cpp">m.<span class="me1">try_lock</span><span class="br0">(</span><span class="br0">)</span></span></span> 拥有下列属性</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>表现为原子操作。</li>
|
|
<li>试图为调用方线程获得互斥的排他性所有权,而不阻塞。若未获得所有权,则立即返回。允许此函数虚假地失败并返回,即使互斥当前未为另一线程所占有。</li>
|
|
<li>若 <code>try_lock()</code> 成功,则先于同一对象上 <code>unlock()</code> 的操作<i>同步于</i>此操作(等价于释放获得 <span class="t-lc"><a href="cpp-atomic-memory_order.html">std::memory_order</a></span> )。 <code>lock()</code> 不与失败的 <code>try_lock()</code> 同步。</li>
|
|
<li>不抛异常。</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
<ul>
|
|
<li>表达式 <span class="t-c"><span class="mw-geshi cpp source-cpp">m.<span class="me1">unlock</span><span class="br0">(</span><span class="br0">)</span></span></span> 拥有下列属性</li>
|
|
</ul>
|
|
<dl>
|
|
<dd>
|
|
<ul>
|
|
<li>表现为原子操作。</li>
|
|
<li>释放调用方线程的互斥所有权,并<i>同步于</i>同一对象上的后继的成功锁操作。</li>
|
|
<li>若调用方线程不占有互斥,则行为未定义。</li>
|
|
<li>不抛异常。</li>
|
|
</ul>
|
|
</dd>
|
|
</dl>
|
|
<ul>
|
|
<li>单个互斥上的所有锁和解锁操作以能视为一个原子变量的<a href="cpp-atomic-memory_order.html#.E4.BF.AE.E6.94.B9.E9.A1.BA.E5.BA.8F">修改顺序</a>的单独全序发生:顺序对此单独互斥是特定的。</li>
|
|
</ul>
|
|
<h3><span class="mw-headline" id=".E5.BA.93.E7.B1.BB.E5.9E.8B">库类型</span></h3>
|
|
<p>下列标准库类型满足<strong class="selflink"><span style="font-style:italic">互斥体</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Mutex)</span></strong> :</p>
|
|
<ul>
|
|
<li><span class="t-lc"><a href="cpp-thread-mutex.html">std::mutex</a></span></li>
|
|
<li><span class="t-lc"><a href="cpp-thread-recursive_mutex.html">std::recursive_mutex</a></span></li>
|
|
<li><span class="t-lc"><a href="cpp-thread-timed_mutex.html">std::timed_mutex</a></span></li>
|
|
<li><span class="t-lc"><a href="cpp-thread-recursive_timed_mutex.html">std::recursive_timed_mutex</a></span></li>
|
|
<li><span class="t-lc"><a href="cpp-thread-shared_mutex.html">std::shared_mutex</a></span></li>
|
|
</ul>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
|
|
<ul>
|
|
<li><a href="cpp-thread.html">线程支持库</a></li>
|
|
<li><a href="cpp-named_req-Lockable.html"><span style="font-style:italic">可锁定</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(Lockable)</span></a></li>
|
|
<li><a href="cpp-named_req-TimedMutex.html"><span style="font-style:italic">定时互斥体</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(TimedMutex)</span></a></li>
|
|
</ul>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|