111 lines
7.7 KiB
HTML
111 lines
7.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>std::unique_lock<Mutex>::try_lock_for</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_thread_unique_lock_try_lock_for 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::unique_lock<Mutex>::</span>try_lock_for</h1>
|
|
<div id="bodyContent">
|
|
<div id="contentSub"><span class="subpages">< <a href="cpp.html">cpp</a>‎ | <a href="cpp-thread.html">thread</a>‎ | <a href="cpp-thread-unique_lock.html">unique lock</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-cxx11">
|
|
<td>
|
|
<div><span class="mw-geshi cpp source-cpp"><span class="kw1">template</span><span class="sy1"><</span> <span class="kw1">class</span> Rep, <span class="kw1">class</span> Period <span class="sy1">></span><br />
|
|
<span class="kw4">bool</span> try_lock_for<span class="br0">(</span> <span class="kw4">const</span> <a href="cpp-chrono-duration.html"><span class="kw993">std::<span class="me2">chrono</span><span class="sy4">::</span><span class="me2">duration</span></span></a><span class="sy1"><</span>Rep,Period<span class="sy1">></span><span class="sy3">&</span> timeout_duration <span class="br0">)</span><span class="sy4">;</span></span></div>
|
|
</td>
|
|
<td class="t-dcl-nopad"></td>
|
|
<td><span class="t-mark-rev t-since-cxx11">(C++11 起)</span></td>
|
|
</tr>
|
|
<tr class="t-dcl-sep">
|
|
<td></td>
|
|
<td></td>
|
|
<td></td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
<p>尝试锁定关联互斥。阻塞直至经过指定的 <code>timeout_duration</code> 或获得锁,之先到来者。成功获得锁时返回 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">true</span></span></span> ,否则返回 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">false</span></span></span> 。等效地调用 <span class="t-c"><span class="mw-geshi cpp source-cpp">mutex<span class="br0">(</span><span class="br0">)</span><span class="sy2">-</span><span class="sy1">></span>try_lock_for<span class="br0">(</span>timeout_duration<span class="br0">)</span></span></span> 。</p>
|
|
<p>由于调度或资源争议延迟,此函数可能阻塞长于 <code>timeout_duration</code> 。</p>
|
|
<p>标准建议用稳定时钟度量时长。若实现用系统时钟代替,则等待时间亦可能对时钟调节敏感。</p>
|
|
<p>若无关联互斥或若此 <code>std::unique_lock</code> 已锁定互斥则抛出 <span class="t-lc"><a href="cpp-error-system_error.html">std::system_error</a></span> 。</p>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E6.95.B0">参数</span></h3>
|
|
<table class="t-par-begin">
|
|
<tr class="t-par">
|
|
<td>timeout_duration</td>
|
|
<td>-</td>
|
|
<td>要阻塞的最大时长</td>
|
|
</tr>
|
|
</table>
|
|
<h3><span class="mw-headline" id=".E8.BF.94.E5.9B.9E.E5.80.BC">返回值</span></h3>
|
|
<p>若成功获得得到互斥锁有权则为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">true</span></span></span> ,否则为 <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">false</span></span></span> 。</p>
|
|
<h3><span class="mw-headline" id=".E5.BC.82.E5.B8.B8">异常</span></h3>
|
|
<ul>
|
|
<li>任何 <span class="t-c"><span class="mw-geshi cpp source-cpp">mutex<span class="br0">(</span><span class="br0">)</span><span class="sy2">-</span><span class="sy1">></span>try_lock_for<span class="br0">(</span>timeout_duration<span class="br0">)</span></span></span> 抛出的异常</li>
|
|
</ul>
|
|
<ul>
|
|
<li>若无关联互斥,则为以 <span class="t-lc"><a href="cpp-error-errc.html">std::errc::operation_not_permitted</a></span> 为错误码的 <span class="t-lc"><a href="cpp-error-system_error.html">std::system_error</a></span></li>
|
|
</ul>
|
|
<ul>
|
|
<li>若已锁定互斥,则为以 <span class="t-lc"><a href="cpp-error-errc.html">std::errc::resource_deadlock_would_occur</a></span> 为错误码的 <span class="t-lc"><a href="cpp-error-system_error.html">std::system_error</a></span></li>
|
|
</ul>
|
|
<h3><span class="mw-headline" id=".E7.A4.BA.E4.BE.8B">示例</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="">本节未完成<br />
|
|
原因:暂无示例</td>
|
|
</tr>
|
|
</table>
|
|
<h3><span class="mw-headline" id=".E5.8F.82.E9.98.85">参阅</span></h3>
|
|
<table class="t-dsc-begin">
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="cpp-thread-unique_lock-lock.html"><span class="t-lines"><span>lock</span></span></a></div>
|
|
</div>
|
|
</td>
|
|
<td>锁定关联互斥<br />
|
|
<span class="t-mark">(公开成员函数)</span></td>
|
|
</tr>
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="cpp-thread-unique_lock-try_lock.html"><span class="t-lines"><span>try_lock</span></span></a></div>
|
|
</div>
|
|
</td>
|
|
<td>尝试锁定关联互斥,若互斥不可用则返回<br />
|
|
<span class="t-mark">(公开成员函数)</span></td>
|
|
</tr>
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="cpp-thread-unique_lock-try_lock_until.html"><span class="t-lines"><span>try_lock_until</span></span></a></div>
|
|
</div>
|
|
</td>
|
|
<td>尝试锁定关联<a href="cpp-named_req-TimedLockable.html"><span style="font-style:italic">可定时锁定</span> <span style="font-family: Georgia, 'DejaVu Serif', serif; font-style:italic">(TimedLockable)</span></a> 互斥,若抵达指定时间点互斥仍不可用则返回<br />
|
|
<span class="t-mark">(公开成员函数)</span></td>
|
|
</tr>
|
|
<tr class="t-dsc">
|
|
<td>
|
|
<div class="t-dsc-member-div">
|
|
<div><a href="cpp-thread-unique_lock-unlock.html"><span class="t-lines"><span>unlock</span></span></a></div>
|
|
</div>
|
|
</td>
|
|
<td>解锁关联互斥<br />
|
|
<span class="t-mark">(公开成员函数)</span></td>
|
|
</tr>
|
|
</table>
|
|
</div>
|
|
<div class="visualClear"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|