Difference between revisions of "Device (C++)"

From Neurotech Software Development Kit
Jump to: navigation, search
(Created page with "<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">Neuro::</span>Device</h1>")
 
Line 1: Line 1:
 
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">Neuro::</span>Device</h1>
 
<h1 id="firstHeading" class="firstHeading"><span style="font-size:0.7em; line-height:130%">Neuro::</span>Device</h1>
 +
<div id="bodyContent">
 +
                                        <!-- tagline -->
 +
                    <div id="siteSub">From cppreference.com</div>
 +
                    <!-- /tagline -->
 +
                                        <!-- subtitle -->
 +
                    <div id="contentSub"><span class="subpages">&lt; <a href="/w/cpp" title="cpp">cpp</a>‎ | <a href="/w/cpp/thread" title="cpp/thread">thread</a></span></div>
 +
                    <!-- /subtitle -->
 +
                                                            <!-- bodycontent -->
 +
                    <div id="mw-content-text" lang="en" dir="ltr" class="mw-content-ltr"><div class="t-navbar" style=""><div class="t-navbar-sep">&nbsp;</div><div class="t-navbar-head"><a href="/w/cpp" title="cpp"> C++</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="line-height:1.1em;">
 +
<tbody><tr class="t-nv"><td colspan="5"> <a href="/w/cpp/language" title="cpp/language"> Language</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/header" title="cpp/header"> Headers</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/concept" title="cpp/concept"> Library concepts</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/utility#Language_support" title="cpp/utility"> Language support library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/error" title="cpp/error"> Diagnostics library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/utility" title="cpp/utility"> Utilities library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/string" title="cpp/string"> Strings library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/container" title="cpp/container"> Containers library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/algorithm" title="cpp/algorithm"> Algorithms library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/iterator" title="cpp/iterator"> Iterators library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/numeric" title="cpp/numeric"> Numerics library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/io" title="cpp/io"> Input/output library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/locale" title="cpp/locale"> Localizations library</a> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/regex" title="cpp/regex"> Regular expressions library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/atomic" title="cpp/atomic"> Atomic operations library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/thread" title="cpp/thread"> Thread support library</a> <span class="t-mark-rev t-since-cxx11">(C++11)</span> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/filesystem" title="cpp/filesystem"> Filesystem library</a> <span class="t-mark-rev t-since-cxx17">(C++17)</span> </td></tr>
 +
<tr class="t-nv"><td colspan="5"> <a href="/w/cpp/experimental" title="cpp/experimental"> Technical Specifications</a> </td></tr>
 +
</tbody></table></div><div><span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/navbar_content&amp;action=edit">[edit]</a></span></div></div></div></div><div class="t-navbar-sep">&nbsp;</div><div class="t-navbar-head"><a href="/w/cpp/thread" title="cpp/thread"> Thread support library</a><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv-h1"><td colspan="5"> Threads</td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/thread" title="cpp/thread/thread"><span class="t-lines"><span>thread</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/hardware_destructive_interference_size" title="cpp/thread/hardware destructive interference size"><span class="t-lines"><span>hardware_destructive_interference_size</span><span>hardware_constructive_interference_size</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv-h2"><td colspan="5"> <code>this_thread</code> namespace</td></tr>
 +
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/get_id" title="cpp/thread/get id"><span class="t-lines"><span>get_id</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/yield" title="cpp/thread/yield"><span class="t-lines"><span>yield</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/sleep_for" title="cpp/thread/sleep for"><span class="t-lines"><span>sleep_for</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/sleep_until" title="cpp/thread/sleep until"><span class="t-lines"><span>sleep_until</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td></tr>
 +
<tr class="t-nv-h1"><td colspan="5"> Mutual exclusion</td></tr>
 +
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><strong class="selflink"><span class="t-lines"><span>mutex</span></span></strong></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/recursive_mutex" title="cpp/thread/recursive mutex"><span class="t-lines"><span>recursive_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/shared_mutex" title="cpp/thread/shared mutex"><span class="t-lines"><span>shared_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/timed_mutex" title="cpp/thread/timed mutex"><span class="t-lines"><span>timed_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/recursive_timed_mutex" title="cpp/thread/recursive timed mutex"><span class="t-lines"><span>recursive_timed_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/shared_timed_mutex" title="cpp/thread/shared timed mutex"><span class="t-lines"><span>shared_timed_mutex</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx14">(C++14)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td></tr>
 +
<tr class="t-nv-h2"><td colspan="5"> Generic lock management</td></tr>
 +
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/lock_guard" title="cpp/thread/lock guard"><span class="t-lines"><span>lock_guard</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/scoped_lock" title="cpp/thread/scoped lock"><span class="t-lines"><span>scoped_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx17">(C++17)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/unique_lock" title="cpp/thread/unique lock"><span class="t-lines"><span>unique_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/shared_lock" title="cpp/thread/shared lock"><span class="t-lines"><span>shared_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx14">(C++14)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/lock_tag_t" title="cpp/thread/lock tag t"><span class="t-lines"><span>defer_lock_t</span><span>try_to_lock_t</span><span>adopt_lock_t</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/lock" title="cpp/thread/lock"><span class="t-lines"><span>lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/try_lock" title="cpp/thread/try lock"><span class="t-lines"><span>try_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/lock_tag" title="cpp/thread/lock tag"><span class="t-lines"><span>defer_lock</span><span>try_to_lock</span><span>adopt_lock</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/once_flag" title="cpp/thread/once flag"><span class="t-lines"><span>once_flag</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/call_once" title="cpp/thread/call once"><span class="t-lines"><span>call_once</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td></tr>
 +
<tr class="t-nv-h1"><td colspan="5"> Condition variables</td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/condition_variable" title="cpp/thread/condition variable"><span class="t-lines"><span>condition_variable</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/condition_variable_any" title="cpp/thread/condition variable any"><span class="t-lines"><span>condition_variable_any</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/notify_all_at_thread_exit" title="cpp/thread/notify all at thread exit"><span class="t-lines"><span>notify_all_at_thread_exit</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/cv_status" title="cpp/thread/cv status"><span class="t-lines"><span>cv_status</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv-h1"><td colspan="5"> Futures</td></tr>
 +
<tr class="t-nv-col-table"><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/promise" title="cpp/thread/promise"><span class="t-lines"><span>promise</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/future" title="cpp/thread/future"><span class="t-lines"><span>future</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/shared_future" title="cpp/thread/shared future"><span class="t-lines"><span>shared_future</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/packaged_task" title="cpp/thread/packaged task"><span class="t-lines"><span>packaged_task</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/async" title="cpp/thread/async"><span class="t-lines"><span>async</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td><td><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/launch" title="cpp/thread/launch"><span class="t-lines"><span>launch</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/future_status" title="cpp/thread/future status"><span class="t-lines"><span>future_status</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/future_error" title="cpp/thread/future error"><span class="t-lines"><span>future_error</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/future_category" title="cpp/thread/future category"><span class="t-lines"><span>future_category</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/future_errc" title="cpp/thread/future errc"><span class="t-lines"><span>future_errc</span></span></a></div><div><span class="t-lines"><span><span class="t-mark-rev t-since-cxx11">(C++11)</span></span></span></div></div></td></tr>
 +
</tbody></table></div></td></tr>
 +
</tbody></table></div><div><span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/navbar_content&amp;action=edit">[edit]</a></span></div></div></div></div><div class="t-navbar-sep">&nbsp;</div><div class="t-navbar-head"><strong class="selflink"><tt>std::mutex</tt></strong><div class="t-navbar-menu"><div><div><table class="t-nv-begin" cellpadding="0" style="">
 +
<tbody><tr class="t-nv-h1"><td colspan="5"> Member functions</td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/mutex/mutex" title="cpp/thread/mutex/mutex"><span class="t-lines"><span>mutex::mutex</span></span></a></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/mutex/%7Emutex" title="cpp/thread/mutex/~mutex"><span class="t-lines"><span>mutex::~mutex</span></span></a></div></div></td></tr>
 +
<tr class="t-nv-h2"><td colspan="5"> Locking</td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"><span class="t-lines"><span>mutex::lock</span></span></a></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"><span class="t-lines"><span>mutex::try_lock</span></span></a></div></div></td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock"><span class="t-lines"><span>mutex::unlock</span></span></a></div></div></td></tr>
 +
<tr class="t-nv-h2"><td colspan="5"> Native handle</td></tr>
 +
<tr class="t-nv"><td colspan="5"><div class="t-nv-ln-table"><div><a href="/w/cpp/thread/mutex/native_handle" title="cpp/thread/mutex/native handle"><span class="t-lines"><span>mutex::native_handle</span></span></a></div></div></td></tr>
 +
</tbody></table></div><div><span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/navbar_content&amp;action=edit">[edit]</a></span></div></div></div></div><div class="t-navbar-sep">&nbsp;</div></div>
 +
<table class="t-dcl-begin"><tbody>
 +
<tr class="t-dsc-header">
 +
<td> <div>Defined in header <code><a href="/w/cpp/header/mutex" title="cpp/header/mutex">&lt;mutex&gt;</a></code>
 +
</div></td>
 +
<td></td>
 +
<td></td>
 +
</tr>
 +
<tr class="t-dcl t-since-cxx11">
 +
<td> <div><span class="mw-geshi cpp source-cpp"><span class="kw1">class</span> mutex<span class="sy4">;</span></span></div></td>
 +
<td class="t-dcl-nopad">  </td>
 +
<td> <span class="t-mark-rev t-since-cxx11">(since C++11)</span> </td>
 +
</tr>
 +
<tr class="t-dcl-sep"><td></td><td></td><td></td></tr>
 +
</tbody></table>
 +
<p>The <code>mutex</code> class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.
 +
</p><p><code>mutex</code> offers exclusive, non-recursive ownership semantics:
 +
</p>
 +
<ul><li> A calling thread <i>owns</i> a <code>mutex</code> from the time that it successfully calls either <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"><code>lock</code></a> or <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"><code>try_lock</code></a> until it calls <a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock"><code>unlock</code></a>.
 +
</li><li> When a thread owns a <code>mutex</code>, all other threads will block (for calls to <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"><code>lock</code></a>) or receive a <span class="t-c"><span class="mw-geshi cpp source-cpp"><span class="kw2">false</span></span></span> return value (for <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"><code>try_lock</code></a>) if they attempt to claim ownership of the <code>mutex</code>.
 +
</li><li> A calling thread must not own the <code>mutex</code> prior to calling <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"><code>lock</code></a> or <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"><code>try_lock</code></a>.
 +
</li></ul>
 +
<p>The behavior of a program is undefined if a <code>mutex</code> is destroyed while still owned by any threads, or a thread terminates while owning a <code>mutex</code>.  The <code>mutex</code> class satisfies all requirements of <a href="/w/cpp/concept/Mutex" title="cpp/concept/Mutex"><code>Mutex</code></a> and <a href="/w/cpp/concept/StandardLayoutType" title="cpp/concept/StandardLayoutType"><code>StandardLayoutType</code></a>.
 +
</p><p><code>std::mutex</code> is neither copyable nor movable.
 +
</p>
 +
<table id="toc" class="toc"><tbody><tr><td><div id="toctitle"><h2>Contents</h2><span class="toctoggle">&nbsp;[<a href="#" class="internal" id="togglelink">hide</a>]&nbsp;</span></div>
 +
<ul>
 +
<li class="toclevel-1 tocsection-1"><a href="#Member_types"><span class="tocnumber">1</span> <span class="toctext">Member types</span></a></li>
 +
<li class="toclevel-1 tocsection-2"><a href="#Member_functions"><span class="tocnumber">2</span> <span class="toctext">Member functions</span></a>
 +
<ul>
 +
<li class="toclevel-2"><a href="#Locking"><span class="tocnumber">2.1</span> <span class="toctext">Locking</span></a></li>
 +
<li class="toclevel-2"><a href="#Native_handle"><span class="tocnumber">2.2</span> <span class="toctext">Native handle</span></a></li>
 +
</ul>
 +
</li>
 +
<li class="toclevel-1 tocsection-3"><a href="#Notes"><span class="tocnumber">3</span> <span class="toctext">Notes</span></a></li>
 +
<li class="toclevel-1 tocsection-4"><a href="#Example"><span class="tocnumber">4</span> <span class="toctext">Example</span></a></li>
 +
</ul>
 +
</td></tr></tbody></table>
 +
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=1" title="Edit section: Member types">edit</a>]</span> <span class="mw-headline" id="Member_types">Member types</span></h3>
 +
<table class="t-dsc-begin">
 +
 +
<tbody><tr class="t-dsc-hitem">
 +
<td>  Member type
 +
</td>
 +
<td>  Definition
 +
</td></tr>
 +
 +
 +
<tr class="t-dsc">
 +
<td>  <code>native_handle_type</code><span class="t-mark">(optional)</span>
 +
</td>
 +
<td>  <i>implementation-defined</i>
 +
</td></tr>
 +
 +
</tbody></table>
 +
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=2" title="Edit section: Member functions">edit</a>]</span> <span class="mw-headline" id="Member_functions">Member functions</span></h3>
 +
<table class="t-dsc-begin">
 +
 +
<tbody><tr class="t-dsc">
 +
<td>  <div class="t-dsc-member-div t-dsc-member-nobold-div"><div><a href="/w/cpp/thread/mutex/mutex" title="cpp/thread/mutex/mutex"> <span class="t-lines"><span>(constructor)</span></span></a></div></div>
 +
</td>
 +
<td>  constructs the mutex <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_constructor&amp;action=edit">[edit]</a></span>
 +
</td></tr>
 +
 +
<tr class="t-dsc">
 +
<td>  <div class="t-dsc-member-div t-dsc-member-nobold-div"><div><a href="/w/cpp/thread/mutex/%7Emutex" title="cpp/thread/mutex/~mutex"> <span class="t-lines"><span>(destructor)</span></span></a></div></div>
 +
</td>
 +
<td>  destroys the mutex <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_destructor&amp;action=edit">[edit]</a></span>
 +
</td></tr>
 +
 +
<tr class="t-dsc">
 +
<td>  <div class="t-dsc-member-div"><div><span class="t-lines"><span>operator=</span></span></div><div><span class="t-lines"><span><span class="t-cmark">[deleted]</span></span></span></div></div>
 +
</td>
 +
<td>  not copy-assignable <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_operator%3D&amp;action=edit">[edit]</a></span>
 +
</td></tr>
 +
 +
 +
<tr>
 +
<td colspan="2"> <h5> <span class="mw-headline" id="Locking">  Locking </span></h5>
 +
</td></tr>
 +
 +
<tr class="t-dsc">
 +
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"> <span class="t-lines"><span>lock</span></span></a></div></div>
 +
</td>
 +
<td>  locks the mutex, blocks if the mutex is not available <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_lock&amp;action=edit">[edit]</a></span>
 +
</td></tr>
 +
 +
<tr class="t-dsc">
 +
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"> <span class="t-lines"><span>try_lock</span></span></a></div></div>
 +
</td>
 +
<td>  tries to lock the mutex, returns if the mutex is not available <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_try_lock&amp;action=edit">[edit]</a></span>
 +
</td></tr>
 +
 +
<tr class="t-dsc">
 +
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock"> <span class="t-lines"><span>unlock</span></span></a></div></div>
 +
</td>
 +
<td>  unlocks the mutex <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_unlock&amp;action=edit">[edit]</a></span>
 +
</td></tr>
 +
 +
 +
<tr>
 +
<td colspan="2"> <h5> <span class="mw-headline" id="Native_handle">  Native handle </span></h5>
 +
</td></tr>
 +
 +
<tr class="t-dsc">
 +
<td>  <div class="t-dsc-member-div"><div><a href="/w/cpp/thread/mutex/native_handle" title="cpp/thread/mutex/native handle"> <span class="t-lines"><span>native_handle</span></span></a></div></div>
 +
</td>
 +
<td>  returns the underlying implementation-defined native handle object <br> <span class="t-mark">(public member function)</span> <span class="editsection noprint plainlinks" title="Edit this template"><a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_native_handle&amp;action=edit">[edit]</a></span>
 +
</td></tr>
 +
</tbody></table>
 +
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=3" title="Edit section: Notes">edit</a>]</span> <span class="mw-headline" id="Notes">Notes</span></h3>
 +
<p><code>std::mutex</code> is usually not accessed directly: <span class="t-lc"><a href="/w/cpp/thread/unique_lock" title="cpp/thread/unique lock">std::unique_lock</a></span>, <span class="t-lc"><a href="/w/cpp/thread/lock_guard" title="cpp/thread/lock guard">std::lock_guard</a></span>, <span class="t-rev-inl t-since-cxx17"><span> or <span class="t-lc">std::scoped_lock</span> </span> <span><span class="t-mark-rev t-since-cxx17">(since C++17)</span></span></span> manage locking in a more exception-safe manner.
 +
</p>
 +
<h3><span class="editsection">[<a href="/mwiki/index.php?title=cpp/thread/mutex&amp;action=edit&amp;section=4" title="Edit section: Example">edit</a>]</span> <span class="mw-headline" id="Example">Example</span></h3>
 +
<div class="t-example"><p> This example shows how a <code>mutex</code> can be used to protect a <span class="t-lc"><a href="/w/cpp/container/map" title="cpp/container/map">std::map</a></span> shared between two threads.
 +
</p><div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div>
 +
<div dir="ltr" class="mw-geshi t-example-code" style="text-align: left;"><div class="cpp source-cpp"><pre class="de1"><span class="co2">#include &lt;iostream&gt;</span>
 +
<span class="co2">#include &lt;map&gt;</span>
 +
<span class="co2">#include &lt;string&gt;</span>
 +
<span class="co2">#include &lt;chrono&gt;</span>
 +
<span class="co2">#include &lt;thread&gt;</span>
 +
<span class="co2">#include &lt;mutex&gt;</span>
 +
&nbsp;
 +
<a href="http://en.cppreference.com/w/cpp/container/map"><span class="kw1281">std::<span class="me2">map</span></span></a><span class="sy1">&lt;</span><a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a>, <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">&gt;</span> g_pages<span class="sy4">;</span>
 +
std<span class="sy4">::</span><span class="me2">mutex</span> g_pages_mutex<span class="sy4">;</span>
 +
&nbsp;
 +
<span class="kw4">void</span> save_page<span class="br0">(</span><span class="kw4">const</span> <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a> <span class="sy3">&amp;</span>url<span class="br0">)</span>
 +
<span class="br0">{</span>
 +
    <span class="co1">// simulate a long page fetch</span>
 +
    <a href="http://en.cppreference.com/w/cpp/thread/sleep_for"><span class="kw2149">std::<span class="me2">this_thread</span><span class="sy4">::</span><span class="me2">sleep_for</span></span></a><span class="br0">(</span><a href="http://en.cppreference.com/w/cpp/chrono/duration"><span class="kw997">std::<span class="me2">chrono</span><span class="sy4">::</span><span class="me2">seconds</span></span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
 +
    <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a> result <span class="sy1">=</span> <span class="st0">"fake content"</span><span class="sy4">;</span>
 +
&nbsp;
 +
    <a href="http://en.cppreference.com/w/cpp/thread/lock_guard"><span class="kw2165">std::<span class="me2">lock_guard</span></span></a><span class="sy1">&lt;</span>std<span class="sy4">::</span><span class="me2">mutex</span><span class="sy1">&gt;</span> guard<span class="br0">(</span>g_pages_mutex<span class="br0">)</span><span class="sy4">;</span>
 +
    g_pages<span class="br0">[</span>url<span class="br0">]</span> <span class="sy1">=</span> result<span class="sy4">;</span>
 +
<span class="br0">}</span>
 +
&nbsp;
 +
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
 +
<span class="br0">{</span>
 +
    <a href="http://en.cppreference.com/w/cpp/thread/thread"><span class="kw2146">std::<span class="me2">thread</span></span></a> t1<span class="br0">(</span>save_page, <span class="st0">"http://foo"</span><span class="br0">)</span><span class="sy4">;</span>
 +
    <a href="http://en.cppreference.com/w/cpp/thread/thread"><span class="kw2146">std::<span class="me2">thread</span></span></a> t2<span class="br0">(</span>save_page, <span class="st0">"http://bar"</span><span class="br0">)</span><span class="sy4">;</span>
 +
    t1.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
 +
    t2.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
 +
&nbsp;
 +
    <span class="co1">// safe to access g_pages without lock now, as the threads are joined</span>
 +
    <span class="kw1">for</span> <span class="br0">(</span><span class="kw4">const</span> <span class="kw4">auto</span> <span class="sy3">&amp;</span>pair <span class="sy4">:</span> g_pages<span class="br0">)</span> <span class="br0">{</span>
 +
        <a href="http://en.cppreference.com/w/cpp/io/cout"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1">&lt;&lt;</span> pair.<span class="me1">first</span> <span class="sy1">&lt;&lt;</span> <span class="st0">" =&gt; "</span> <span class="sy1">&lt;&lt;</span> pair.<span class="me1">second</span> <span class="sy1">&lt;&lt;</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
 +
    <span class="br0">}</span>
 +
<span class="br0">}</span></pre></div></div>
 +
<p>Output:
 +
</p>
 +
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="text source-text"><pre class="de1">http://bar =&gt; fake content
 +
http://foo =&gt; fake content</pre></div></div>
 +
</div>
 +
 +
<!--
 +
NewPP limit report
 +
Preprocessor visited node count: 5285/1000000
 +
Preprocessor generated node count: 10355/1000000
 +
Post‐expand include size: 159462/2097152 bytes
 +
Template argument size: 30567/2097152 bytes
 +
Highest expansion depth: 20/40
 +
Expensive parser function count: 0/100
 +
-->
 +
 +
<!-- Saved in parser cache with key mwiki1-mwiki_en_:pcache:idhash:2291-0!*!0!!en!*!* and timestamp 20180411183737 -->
 +
</div>                    <!-- /bodycontent -->
 +
                                        <!-- printfooter -->
 +
                    <div class="printfooter">
 +
                    Retrieved from "<a href="http://en.cppreference.com/mwiki/index.php?title=cpp/thread/mutex&amp;oldid=89024">http://en.cppreference.com/mwiki/index.php?title=cpp/thread/mutex&amp;oldid=89024</a>"                    </div>
 +
                    <!-- /printfooter -->
 +
                                                            <!-- catlinks -->
 +
                    <div id="catlinks" class="catlinks catlinks-allhidden"></div>                    <!-- /catlinks -->
 +
                                                            <div class="visualClear"></div>
 +
                    <!-- debughtml -->
 +
                                        <!-- /debughtml -->
 +
                </div>

Revision as of 07:33, 25 April 2018

Neuro::Device

From cppreference.com
< <a href="/w/cpp" title="cpp">cpp</a>‎ | <a href="/w/cpp/thread" title="cpp/thread">thread</a>
 
<a href="/w/cpp" title="cpp"> C++</a>
<tbody> </tbody>
<a href="/w/cpp/language" title="cpp/language"> Language</a>
<a href="/w/cpp/header" title="cpp/header"> Headers</a>
<a href="/w/cpp/concept" title="cpp/concept"> Library concepts</a>
<a href="/w/cpp/utility#Language_support" title="cpp/utility"> Language support library</a>
<a href="/w/cpp/error" title="cpp/error"> Diagnostics library</a>
<a href="/w/cpp/utility" title="cpp/utility"> Utilities library</a>
<a href="/w/cpp/string" title="cpp/string"> Strings library</a>
<a href="/w/cpp/container" title="cpp/container"> Containers library</a>
<a href="/w/cpp/algorithm" title="cpp/algorithm"> Algorithms library</a>
<a href="/w/cpp/iterator" title="cpp/iterator"> Iterators library</a>
<a href="/w/cpp/numeric" title="cpp/numeric"> Numerics library</a>
<a href="/w/cpp/io" title="cpp/io"> Input/output library</a>
<a href="/w/cpp/locale" title="cpp/locale"> Localizations library</a>
<a href="/w/cpp/regex" title="cpp/regex"> Regular expressions library</a> (C++11)
<a href="/w/cpp/atomic" title="cpp/atomic"> Atomic operations library</a> (C++11)
<a href="/w/cpp/thread" title="cpp/thread"> Thread support library</a> (C++11)
<a href="/w/cpp/filesystem" title="cpp/filesystem"> Filesystem library</a> (C++17)
<a href="/w/cpp/experimental" title="cpp/experimental"> Technical Specifications</a>
 
<a href="/w/cpp/thread" title="cpp/thread"> Thread support library</a>
<tbody> </tbody>
Threads
<a href="/w/cpp/thread/thread" title="cpp/thread/thread">thread</a>
(C++11)
<a href="/w/cpp/thread/hardware_destructive_interference_size" title="cpp/thread/hardware destructive interference size">hardware_destructive_interference_sizehardware_constructive_interference_size</a>
(C++17)(C++17)
this_thread namespace
<tbody> </tbody>
<a href="/w/cpp/thread/get_id" title="cpp/thread/get id">get_id</a>
(C++11)
<a href="/w/cpp/thread/yield" title="cpp/thread/yield">yield</a>
(C++11)
<tbody> </tbody>
<a href="/w/cpp/thread/sleep_for" title="cpp/thread/sleep for">sleep_for</a>
(C++11)
<a href="/w/cpp/thread/sleep_until" title="cpp/thread/sleep until">sleep_until</a>
(C++11)
Mutual exclusion
<tbody> </tbody>
mutex
(C++11)
<a href="/w/cpp/thread/recursive_mutex" title="cpp/thread/recursive mutex">recursive_mutex</a>
(C++11)
<a href="/w/cpp/thread/shared_mutex" title="cpp/thread/shared mutex">shared_mutex</a>
(C++17)
<tbody> </tbody>
<a href="/w/cpp/thread/timed_mutex" title="cpp/thread/timed mutex">timed_mutex</a>
(C++11)
<a href="/w/cpp/thread/recursive_timed_mutex" title="cpp/thread/recursive timed mutex">recursive_timed_mutex</a>
(C++11)
<a href="/w/cpp/thread/shared_timed_mutex" title="cpp/thread/shared timed mutex">shared_timed_mutex</a>
(C++14)
Generic lock management
<tbody> </tbody>
<a href="/w/cpp/thread/lock_guard" title="cpp/thread/lock guard">lock_guard</a>
(C++11)
<a href="/w/cpp/thread/scoped_lock" title="cpp/thread/scoped lock">scoped_lock</a>
(C++17)
<a href="/w/cpp/thread/unique_lock" title="cpp/thread/unique lock">unique_lock</a>
(C++11)
<a href="/w/cpp/thread/shared_lock" title="cpp/thread/shared lock">shared_lock</a>
(C++14)
<a href="/w/cpp/thread/lock_tag_t" title="cpp/thread/lock tag t">defer_lock_ttry_to_lock_tadopt_lock_t</a>
(C++11)(C++11)(C++11)
<tbody> </tbody>
<a href="/w/cpp/thread/lock" title="cpp/thread/lock">lock</a>
(C++11)
<a href="/w/cpp/thread/try_lock" title="cpp/thread/try lock">try_lock</a>
(C++11)
<a href="/w/cpp/thread/lock_tag" title="cpp/thread/lock tag">defer_locktry_to_lockadopt_lock</a>
(C++11)(C++11)(C++11)
<a href="/w/cpp/thread/once_flag" title="cpp/thread/once flag">once_flag</a>
(C++11)
<a href="/w/cpp/thread/call_once" title="cpp/thread/call once">call_once</a>
(C++11)
Condition variables
<a href="/w/cpp/thread/condition_variable" title="cpp/thread/condition variable">condition_variable</a>
(C++11)
<a href="/w/cpp/thread/condition_variable_any" title="cpp/thread/condition variable any">condition_variable_any</a>
(C++11)
<a href="/w/cpp/thread/notify_all_at_thread_exit" title="cpp/thread/notify all at thread exit">notify_all_at_thread_exit</a>
(C++11)
<a href="/w/cpp/thread/cv_status" title="cpp/thread/cv status">cv_status</a>
(C++11)
Futures
<tbody> </tbody>
<a href="/w/cpp/thread/promise" title="cpp/thread/promise">promise</a>
(C++11)
<a href="/w/cpp/thread/future" title="cpp/thread/future">future</a>
(C++11)
<a href="/w/cpp/thread/shared_future" title="cpp/thread/shared future">shared_future</a>
(C++11)
<a href="/w/cpp/thread/packaged_task" title="cpp/thread/packaged task">packaged_task</a>
(C++11)
<a href="/w/cpp/thread/async" title="cpp/thread/async">async</a>
(C++11)
<tbody> </tbody>
<a href="/w/cpp/thread/launch" title="cpp/thread/launch">launch</a>
(C++11)
<a href="/w/cpp/thread/future_status" title="cpp/thread/future status">future_status</a>
(C++11)
<a href="/w/cpp/thread/future_error" title="cpp/thread/future error">future_error</a>
(C++11)
<a href="/w/cpp/thread/future_category" title="cpp/thread/future category">future_category</a>
(C++11)
<a href="/w/cpp/thread/future_errc" title="cpp/thread/future errc">future_errc</a>
(C++11)
 
std::mutex
<tbody> </tbody>
Member functions
<a href="/w/cpp/thread/mutex/mutex" title="cpp/thread/mutex/mutex">mutex::mutex</a>
<a href="/w/cpp/thread/mutex/%7Emutex" title="cpp/thread/mutex/~mutex">mutex::~mutex</a>
Locking
<a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock">mutex::lock</a>
<a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock">mutex::try_lock</a>
<a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock">mutex::unlock</a>
Native handle
<a href="/w/cpp/thread/mutex/native_handle" title="cpp/thread/mutex/native handle">mutex::native_handle</a>
 
<tbody> </tbody>
Defined in header <a href="/w/cpp/header/mutex" title="cpp/header/mutex"><mutex></a>
class mutex;
(since C++11)

The mutex class is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads.

mutex offers exclusive, non-recursive ownership semantics:

  • A calling thread owns a mutex from the time that it successfully calls either <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock">lock</a> or <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock">try_lock</a> until it calls <a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock">unlock</a>.
  • When a thread owns a mutex, all other threads will block (for calls to <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock">lock</a>) or receive a false return value (for <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock">try_lock</a>) if they attempt to claim ownership of the mutex.
  • A calling thread must not own the mutex prior to calling <a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock">lock</a> or <a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock">try_lock</a>.

The behavior of a program is undefined if a mutex is destroyed while still owned by any threads, or a thread terminates while owning a mutex. The mutex class satisfies all requirements of <a href="/w/cpp/concept/Mutex" title="cpp/concept/Mutex">Mutex</a> and <a href="/w/cpp/concept/StandardLayoutType" title="cpp/concept/StandardLayoutType">StandardLayoutType</a>.

std::mutex is neither copyable nor movable.

<tbody></tbody>

Contents

 [<a href="#" class="internal" id="togglelink">hide</a>] 
  • <a href="#Member_types">1 Member types</a>
  • <a href="#Member_functions">2 Member functions</a>
    • <a href="#Locking">2.1 Locking</a>
    • <a href="#Native_handle">2.2 Native handle</a>
  • <a href="#Notes">3 Notes</a>
  • <a href="#Example">4 Example</a>

[<a href="/mwiki/index.php?title=cpp/thread/mutex&action=edit&section=1" title="Edit section: Member types">edit</a>] Member types

<tbody> </tbody>
Member type Definition
native_handle_type(optional) implementation-defined

[<a href="/mwiki/index.php?title=cpp/thread/mutex&action=edit&section=2" title="Edit section: Member functions">edit</a>] Member functions

<tbody> </tbody>
<a href="/w/cpp/thread/mutex/mutex" title="cpp/thread/mutex/mutex"> (constructor)</a>
constructs the mutex
(public member function) <a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_constructor&action=edit">[edit]</a>
<a href="/w/cpp/thread/mutex/%7Emutex" title="cpp/thread/mutex/~mutex"> (destructor)</a>
destroys the mutex
(public member function) <a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_destructor&action=edit">[edit]</a>
operator=
[deleted]
not copy-assignable
(public member function) <a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_operator%3D&action=edit">[edit]</a>
Locking
<a href="/w/cpp/thread/mutex/lock" title="cpp/thread/mutex/lock"> lock</a>
locks the mutex, blocks if the mutex is not available
(public member function) <a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_lock&action=edit">[edit]</a>
<a href="/w/cpp/thread/mutex/try_lock" title="cpp/thread/mutex/try lock"> try_lock</a>
tries to lock the mutex, returns if the mutex is not available
(public member function) <a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_try_lock&action=edit">[edit]</a>
<a href="/w/cpp/thread/mutex/unlock" title="cpp/thread/mutex/unlock"> unlock</a>
unlocks the mutex
(public member function) <a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_unlock&action=edit">[edit]</a>
Native handle
<a href="/w/cpp/thread/mutex/native_handle" title="cpp/thread/mutex/native handle"> native_handle</a>
returns the underlying implementation-defined native handle object
(public member function) <a rel="nofollow" class="external text" href="http://en.cppreference.com/mwiki/index.php?title=Template:cpp/thread/mutex/dsc_native_handle&action=edit">[edit]</a>

[<a href="/mwiki/index.php?title=cpp/thread/mutex&action=edit&section=3" title="Edit section: Notes">edit</a>] Notes

std::mutex is usually not accessed directly: <a href="/w/cpp/thread/unique_lock" title="cpp/thread/unique lock">std::unique_lock</a>, <a href="/w/cpp/thread/lock_guard" title="cpp/thread/lock guard">std::lock_guard</a>, or std::scoped_lock (since C++17) manage locking in a more exception-safe manner.

[<a href="/mwiki/index.php?title=cpp/thread/mutex&action=edit&section=4" title="Edit section: Example">edit</a>] Example

This example shows how a mutex can be used to protect a <a href="/w/cpp/container/map" title="cpp/container/map">std::map</a> shared between two threads.

<span class="co2">#include <iostream></span>
<span class="co2">#include <map></span>
<span class="co2">#include <string></span>
<span class="co2">#include <chrono></span>
<span class="co2">#include <thread></span>
<span class="co2">#include <mutex></span>
 
<a href="http://en.cppreference.com/w/cpp/container/map"><span class="kw1281">std::<span class="me2">map</span></span></a><span class="sy1"><</span><a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a>, <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a><span class="sy1">></span> g_pages<span class="sy4">;</span>
std<span class="sy4">::</span><span class="me2">mutex</span> g_pages_mutex<span class="sy4">;</span>
 
<span class="kw4">void</span> save_page<span class="br0">(</span><span class="kw4">const</span> <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a> <span class="sy3">&</span>url<span class="br0">)</span>
<span class="br0">{</span>
    <span class="co1">// simulate a long page fetch</span>
    <a href="http://en.cppreference.com/w/cpp/thread/sleep_for"><span class="kw2149">std::<span class="me2">this_thread</span><span class="sy4">::</span><span class="me2">sleep_for</span></span></a><span class="br0">(</span><a href="http://en.cppreference.com/w/cpp/chrono/duration"><span class="kw997">std::<span class="me2">chrono</span><span class="sy4">::</span><span class="me2">seconds</span></span></a><span class="br0">(</span><span class="nu0">2</span><span class="br0">)</span><span class="br0">)</span><span class="sy4">;</span>
    <a href="http://en.cppreference.com/w/cpp/string/basic_string"><span class="kw1230">std::<span class="me2">string</span></span></a> result <span class="sy1">=</span> <span class="st0">"fake content"</span><span class="sy4">;</span>
 
    <a href="http://en.cppreference.com/w/cpp/thread/lock_guard"><span class="kw2165">std::<span class="me2">lock_guard</span></span></a><span class="sy1"><</span>std<span class="sy4">::</span><span class="me2">mutex</span><span class="sy1">></span> guard<span class="br0">(</span>g_pages_mutex<span class="br0">)</span><span class="sy4">;</span>
    g_pages<span class="br0">[</span>url<span class="br0">]</span> <span class="sy1">=</span> result<span class="sy4">;</span>
<span class="br0">}</span>
 
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span> 
<span class="br0">{</span>
    <a href="http://en.cppreference.com/w/cpp/thread/thread"><span class="kw2146">std::<span class="me2">thread</span></span></a> t1<span class="br0">(</span>save_page, <span class="st0">"http://foo"</span><span class="br0">)</span><span class="sy4">;</span>
    <a href="http://en.cppreference.com/w/cpp/thread/thread"><span class="kw2146">std::<span class="me2">thread</span></span></a> t2<span class="br0">(</span>save_page, <span class="st0">"http://bar"</span><span class="br0">)</span><span class="sy4">;</span>
    t1.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
    t2.<span class="me1">join</span><span class="br0">(</span><span class="br0">)</span><span class="sy4">;</span>
 
    <span class="co1">// safe to access g_pages without lock now, as the threads are joined</span>
    <span class="kw1">for</span> <span class="br0">(</span><span class="kw4">const</span> <span class="kw4">auto</span> <span class="sy3">&</span>pair <span class="sy4">:</span> g_pages<span class="br0">)</span> <span class="br0">{</span>
        <a href="http://en.cppreference.com/w/cpp/io/cout"><span class="kw1758">std::<span class="me2">cout</span></span></a> <span class="sy1"><<</span> pair.<span class="me1">first</span> <span class="sy1"><<</span> <span class="st0">" => "</span> <span class="sy1"><<</span> pair.<span class="me1">second</span> <span class="sy1"><<</span> <span class="st0">'<span class="es1">\n</span>'</span><span class="sy4">;</span>
    <span class="br0">}</span>
<span class="br0">}</span>

Output:

http://bar => fake content
http://foo => fake content