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

From Neurotech Software Development Kit
Jump to: navigation, search
(Neuro::Device)
(Neuro::Device)
Line 7: Line 7:
  
 
The Device class is an abstraction for NeuroMD BLE devices. This abstraction provides functions for changing of device state by executing commands and setting parameters. Each device have different sets of supported commands and parameters, Device class has functions designed to get information about these sets.
 
The Device class is an abstraction for NeuroMD BLE devices. This abstraction provides functions for changing of device state by executing commands and setting parameters. Each device have different sets of supported commands and parameters, Device class has functions designed to get information about these sets.
<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">
+
==Member functions==
<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">
 
<table class="t-dsc-begin">
  
Line 102: Line 64:
 
</td></tr>
 
</td></tr>
 
</tbody></table>
 
</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>
 
<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><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.

Revision as of 09:46, 25 April 2018

Neuro::Device

Defined in header <device/device.h>
class Device final;

The Device class is an abstraction for NeuroMD BLE devices. This abstraction provides functions for changing of device state by executing commands and setting parameters. Each device have different sets of supported commands and parameters, Device class has functions designed to get information about these sets.

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


</div>

               </div>