Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-release > by-pkgid > 3b996fe272685bfa229e6a8dcb542001 > files > 1269

glibmm2.4-doc-2.60.0-1.mga7.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>glibmm: thread/thread.cc</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="doxygen-extra.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">glibmm
   &#160;<span id="projectnumber">2.60.0</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">thread/thread.cc</div>  </div>
</div><!--header-->
<div class="contents">
<p>A glibmm thread example.</p>
<div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00059.html">condition_variable</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00086.html">iostream</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00107.html">memory</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00110.html">mutex</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00119.html">queue</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a00158.html">thread</a>&gt;</span></div><div class="line"><span class="preprocessor">#if defined(_MSC_VER) &amp;&amp; (_MSC_VER &lt; 1900)</span></div><div class="line"><span class="comment">/* For using noexcept on Visual Studio 2013 */</span></div><div class="line"><span class="preprocessor">#include &lt;glibmmconfig.h&gt;</span></div><div class="line"><span class="preprocessor">#endif</span></div><div class="line"><span class="preprocessor">#include &lt;glibmm/init.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;glibmm/random.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;glibmm/timer.h&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">namespace</span></div><div class="line">{</div><div class="line"></div><div class="line"><span class="keyword">class </span>MessageQueue</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line">  MessageQueue();</div><div class="line">  ~MessageQueue();</div><div class="line"></div><div class="line">  <span class="keywordtype">void</span> producer();</div><div class="line">  <span class="keywordtype">void</span> consumer();</div><div class="line"></div><div class="line"><span class="keyword">private</span>:</div><div class="line">  <a name="_a0"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a06240.html">std::mutex</a> mutex_;</div><div class="line">  <a name="_a1"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a02056.html">std::condition_variable</a> cond_push_;</div><div class="line">  <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a02056.html">std::condition_variable</a> cond_pop_;</div><div class="line">  <a name="_a2"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a06760.html">std::queue&lt;int&gt;</a> queue_;</div><div class="line">};</div><div class="line"></div><div class="line">MessageQueue::MessageQueue()</div><div class="line">{</div><div class="line">}</div><div class="line"></div><div class="line">MessageQueue::~MessageQueue()</div><div class="line">{</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span></div><div class="line">MessageQueue::producer()</div><div class="line">{</div><div class="line">  <a name="_a3"></a><a class="code" href="classGlib_1_1Rand.html">Glib::Rand</a> rand(1234);</div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (<span class="keyword">auto</span> i = 0; i &lt; 200; ++i)</div><div class="line">  {</div><div class="line">    {</div><div class="line">      <a name="_a4"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a06260.html">std::unique_lock&lt;std::mutex&gt;</a> <a name="a5"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#a82fa606b1427f731ac64bb8ac49221f3">lock</a>(mutex_);</div><div class="line"></div><div class="line">      cond_pop_.wait(lock, [<span class="keyword">this</span>]() -&gt; <span class="keywordtype">bool</span> { <span class="keywordflow">return</span> queue_.size() &lt; 64; });</div><div class="line"></div><div class="line">      queue_.push(i);</div><div class="line">      <a name="a6"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#aaf93fdf0812752e0e02c501dea1b38f0">std::cout</a> &lt;&lt; <span class="charliteral">&#39;*&#39;</span>;</div><div class="line">      <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#aaf93fdf0812752e0e02c501dea1b38f0">std::cout</a>.<a name="a7"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a02576.html#ab5155ad385b78192ef1436bf31a0cde0">flush</a>();</div><div class="line"></div><div class="line">      <span class="comment">// We unlock before notifying, because that is what the documentation suggests:</span></div><div class="line">      <span class="comment">// http://en.cppreference.com/w/cpp/thread/condition_variable</span></div><div class="line">      lock.unlock();</div><div class="line">      cond_push_.notify_one();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (rand.<a name="a8"></a><a class="code" href="classGlib_1_1Rand.html#a4d90f1e3ef59d0b071efa0445b6fb469">get_bool</a>())</div><div class="line">      <span class="keywordflow">continue</span>;</div><div class="line"></div><div class="line">    <a name="a9"></a><a class="code" href="namespaceGlib.html#a5d75264c8b47f13fb33f973b0d4fc73e">Glib::usleep</a>(rand.<a name="a10"></a><a class="code" href="classGlib_1_1Rand.html#a182b9a36db153d316baf5efb1ece9fd7">get_int_range</a>(0, 100000));</div><div class="line">  }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span></div><div class="line">MessageQueue::consumer()</div><div class="line">{</div><div class="line">  <a class="code" href="classGlib_1_1Rand.html">Glib::Rand</a> rand(4567);</div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (;;)</div><div class="line">  {</div><div class="line">    {</div><div class="line">      <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a06260.html">std::unique_lock&lt;std::mutex&gt;</a> <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#a82fa606b1427f731ac64bb8ac49221f3">lock</a>(mutex_);</div><div class="line"></div><div class="line">      cond_push_.wait(lock, [<span class="keyword">this</span>]() -&gt; <span class="keywordtype">bool</span> { <span class="keywordflow">return</span> !queue_.empty(); });</div><div class="line"></div><div class="line">      <span class="keyword">const</span> <span class="keywordtype">int</span> i = queue_.front();</div><div class="line">      queue_.pop();</div><div class="line">      <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#aaf93fdf0812752e0e02c501dea1b38f0">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;\x08 \x08&quot;</span>;</div><div class="line">      <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#aaf93fdf0812752e0e02c501dea1b38f0">std::cout</a>.<a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a02576.html#ab5155ad385b78192ef1436bf31a0cde0">flush</a>();</div><div class="line"></div><div class="line">      <span class="comment">// We unlock before notifying, because that is what the documentation suggests:</span></div><div class="line">      <span class="comment">// http://en.cppreference.com/w/cpp/thread/condition_variable</span></div><div class="line">      lock.unlock();</div><div class="line">      cond_pop_.notify_one();</div><div class="line"></div><div class="line">      <span class="keywordflow">if</span> (i &gt;= 199)</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (rand.<a class="code" href="classGlib_1_1Rand.html#a4d90f1e3ef59d0b071efa0445b6fb469">get_bool</a>())</div><div class="line">      <span class="keywordflow">continue</span>;</div><div class="line"></div><div class="line">    <a class="code" href="namespaceGlib.html#a5d75264c8b47f13fb33f973b0d4fc73e">Glib::usleep</a>(rand.<a class="code" href="classGlib_1_1Rand.html#a182b9a36db153d316baf5efb1ece9fd7">get_int_range</a>(10000, 200000));</div><div class="line">  }</div><div class="line">}</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span></div><div class="line">main(<span class="keywordtype">int</span>, <span class="keywordtype">char</span>**)</div><div class="line">{</div><div class="line">  <a name="a11"></a><a class="code" href="namespaceGlib.html#ac90aee10d0b90e3d8a96a86b5394f87b">Glib::init</a>();</div><div class="line"></div><div class="line">  MessageQueue queue;</div><div class="line"></div><div class="line">  <span class="comment">// TODO: Use std::make_unique() when we use C++14:</span></div><div class="line">  <span class="keyword">const</span> <span class="keyword">auto</span> producer =</div><div class="line">    <a name="_a12"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a06956.html">std::unique_ptr&lt;std::thread&gt;</a>(<span class="keyword">new</span> <a name="_a13"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a02972.html">std::thread</a>(&amp;MessageQueue::producer, &amp;queue));</div><div class="line"></div><div class="line">  <span class="keyword">const</span> <span class="keyword">auto</span> consumer =</div><div class="line">    <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a06956.html">std::unique_ptr&lt;std::thread&gt;</a>(<span class="keyword">new</span> <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a02972.html">std::thread</a>(&amp;MessageQueue::consumer, &amp;queue));</div><div class="line"></div><div class="line">  producer-&gt;join();</div><div class="line">  consumer-&gt;join();</div><div class="line"></div><div class="line">  <a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#aaf93fdf0812752e0e02c501dea1b38f0">std::cout</a> &lt;&lt; <a name="a14"></a><a class="codeRef" href="http://gcc.gnu.org/onlinedocs/libstdc++/latest-doxygen/a01544.html#a8b3758bb8f17c440c7963363f42f69f3">std::endl</a>;</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Tue Mar 19 2019 09:56:37 for glibmm by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>