Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/queue_8cpp_source.html
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 13 Aug 2010 16:47:46 +0100
changeset 14 578be2adaf3e
parent 6 43e37759235e
permissions -rw-r--r--
Week 32 contribution of PDK documentation content. See release notes for details. Fixes bug Bug 3582

<!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"/>
<title>TB9.2 Example Applications: examples/Base/IPC/condvar/condvarlocal/src/queue.cpp Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2 -->
<h1>examples/Base/IPC/condvar/condvarlocal/src/queue.cpp</h1><a href="queue_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
<a name="l00004"></a>00004 <span class="comment">// under the terms of &quot;Eclipse Public License v1.0&quot;</span>
<a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
<a name="l00006"></a>00006 <span class="comment">// at the URL &quot;http://www.eclipse.org/legal/epl-v10.html&quot;.</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
<a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
<a name="l00010"></a>00010 <span class="comment">//</span>
<a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// Description:</span>
<a name="l00014"></a>00014 <span class="comment">// Contains the definition of member functions of the CQueue class.</span>
<a name="l00015"></a>00015 <span class="comment">//</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 
<a name="l00018"></a>00018 
<a name="l00023"></a>00023 <span class="preprocessor">#include &quot;<a class="code" href="queue_8h.html">queue.h</a>&quot;</span>
<a name="l00024"></a>00024 
<a name="l00029"></a><a class="code" href="class_c_queue.html#a2c3c0bae6b0e6aaa10df8fb51dfb7e30">00029</a> <a class="code" href="class_c_queue.html">CQueue</a>* <a class="code" href="class_c_queue.html#a2c3c0bae6b0e6aaa10df8fb51dfb7e30">CQueue::NewL</a>()
<a name="l00030"></a>00030         {
<a name="l00031"></a>00031         <a class="code" href="class_c_queue.html">CQueue</a>* <span class="keyword">self</span> = <span class="keyword">new</span> (ELeave)<a class="code" href="class_c_queue.html">CQueue</a>;
<a name="l00032"></a>00032         CleanupStack::PushL(<span class="keyword">self</span>);
<a name="l00033"></a>00033         <span class="keyword">self</span>-&gt;ConstructL();
<a name="l00034"></a>00034         CleanupStack::Pop(<span class="keyword">self</span>);
<a name="l00035"></a>00035         <span class="keywordflow">return</span> <span class="keyword">self</span>;
<a name="l00036"></a>00036         }
<a name="l00037"></a>00037 
<a name="l00043"></a><a class="code" href="class_c_queue.html#a543c0e6f1fdcb5ff5382f50020e7c425">00043</a> <span class="keywordtype">void</span> <a class="code" href="class_c_queue.html#a543c0e6f1fdcb5ff5382f50020e7c425">CQueue::ConstructL</a>()
<a name="l00044"></a>00044         {
<a name="l00045"></a>00045         <span class="comment">// Create the local RMutex variable.</span>
<a name="l00046"></a>00046         User::LeaveIfError(iMutex.CreateLocal());
<a name="l00047"></a>00047         <span class="comment">// Create the local RCondVar variable.</span>
<a name="l00048"></a>00048         User::LeaveIfError(iCondVar.CreateLocal());
<a name="l00049"></a>00049         }
<a name="l00050"></a>00050 
<a name="l00054"></a>00054 CQueue::CQueue()
<a name="l00055"></a>00055         {
<a name="l00056"></a>00056         }
<a name="l00057"></a>00057 
<a name="l00062"></a><a class="code" href="class_c_queue.html#acf8845b17955c6cf4e6b779630aa7f50">00062</a> <span class="keywordtype">void</span> <a class="code" href="class_c_queue.html#acf8845b17955c6cf4e6b779630aa7f50">CQueue::Insert</a>()
<a name="l00063"></a>00063         {
<a name="l00064"></a>00064         <span class="comment">// Wait for the mutex.</span>
<a name="l00065"></a>00065         <span class="comment">// The mutex is required to ensure that only one thread updates iArray at a time.</span>
<a name="l00066"></a>00066         iMutex.Wait();
<a name="l00067"></a>00067         <span class="comment">// Insertion operation.</span>
<a name="l00068"></a>00068         <span class="comment">// Insert some random number as a token into the queue.</span>
<a name="l00069"></a>00069         iArray.Append(Math::Random()%10);
<a name="l00070"></a>00070         <span class="comment">// Signal the local condition variable to indicate that queue has new data.</span>
<a name="l00071"></a>00071         iCondVar.Signal();
<a name="l00072"></a>00072         <span class="comment">// release the mutex so that the other thread has access to iArray.</span>
<a name="l00073"></a>00073         iMutex.Signal();
<a name="l00074"></a>00074         <span class="comment">// RCondVar::Broadcast() can be used as an alternative to the RCondVar::Signal() function.</span>
<a name="l00075"></a>00075         <span class="comment">// It is more relevant to use Broadcast() when more than two threads are blocking on single condition variable.</span>
<a name="l00076"></a>00076         <span class="comment">// The following code block illustrates the use of the Broadcast() function.</span>
<a name="l00077"></a>00077         <span class="comment">//</span>
<a name="l00078"></a>00078         <span class="comment">//if(!IsEmpty())</span>
<a name="l00079"></a>00079         <span class="comment">//      {</span>
<a name="l00080"></a>00080         <span class="comment">//      iCondVar.Broadcast();</span>
<a name="l00081"></a>00081         <span class="comment">//      iMutex.Signal();</span>
<a name="l00082"></a>00082         <span class="comment">//      }</span>
<a name="l00083"></a>00083         <span class="comment">//</span>
<a name="l00084"></a>00084         }
<a name="l00085"></a>00085 
<a name="l00090"></a><a class="code" href="class_c_queue.html#ab95dcac0beafcc90e1d2cdad2edfb35d">00090</a> <span class="keywordtype">void</span> <a class="code" href="class_c_queue.html#ab95dcac0beafcc90e1d2cdad2edfb35d">CQueue::Remove</a>()
<a name="l00091"></a>00091         {
<a name="l00092"></a>00092         <span class="comment">// Ensures that mutex is held by the current thread.</span>
<a name="l00093"></a>00093         iMutex.Wait();
<a name="l00094"></a>00094         <span class="comment">// Test to see whether there is anything in the queue before attempting to remove an item.</span>
<a name="l00095"></a>00095         <span class="comment">// If the queue is empty wait for a signal on the Condition Variable before proceeding</span>
<a name="l00096"></a>00096         <span class="keywordflow">while</span>(!<a class="code" href="class_c_queue.html#ab2c462ec968532861415e05e5f10b873">IsEmpty</a>())
<a name="l00097"></a>00097                 {
<a name="l00098"></a>00098                 <span class="comment">// The following call blocks this thread and releases the mutex so that the other thread can update the shared data.</span>
<a name="l00099"></a>00099                 <span class="comment">// When the other thread signals iCondVar and releases the mutex this thread continues.  </span>
<a name="l00100"></a>00100                 iCondVar.Wait(iMutex);
<a name="l00101"></a>00101                 <span class="comment">// If there were more than one consumer waiting for the same signal</span>
<a name="l00102"></a>00102                 <span class="comment">// it would be essential to test IsEmpty() again because another thread, or threads,</span>
<a name="l00103"></a>00103                 <span class="comment">// might have got the mutex before this one and emptied the array.</span>
<a name="l00104"></a>00104                 <span class="comment">// For this reason if is good practise to use while( IsEmpty() ) instead of if( IsEmpty() ).</span>
<a name="l00105"></a>00105                 }
<a name="l00106"></a>00106         <span class="comment">// Deletion operation.</span>
<a name="l00107"></a>00107         iArray.Remove(0);
<a name="l00108"></a>00108         <span class="comment">// Release the mutex.</span>
<a name="l00109"></a>00109         iMutex.Signal();
<a name="l00110"></a>00110         }
<a name="l00111"></a>00111 
<a name="l00116"></a><a class="code" href="class_c_queue.html#ab2c462ec968532861415e05e5f10b873">00116</a> TBool <a class="code" href="class_c_queue.html#ab2c462ec968532861415e05e5f10b873">CQueue::IsEmpty</a>()
<a name="l00117"></a>00117         {
<a name="l00118"></a>00118         <span class="keywordflow">if</span>(iArray.Count() &gt; 0)
<a name="l00119"></a>00119                 {
<a name="l00120"></a>00120                 <span class="keywordflow">return</span> ETrue;
<a name="l00121"></a>00121                 }
<a name="l00122"></a>00122         <span class="keywordflow">return</span> EFalse;
<a name="l00123"></a>00123         }
<a name="l00124"></a>00124 
<a name="l00129"></a><a class="code" href="class_c_queue.html#ab4d7758aee550b3f77a800959979e9fd">00129</a> <span class="keywordtype">void</span> <a class="code" href="class_c_queue.html#ab4d7758aee550b3f77a800959979e9fd">CQueue::GetTokens</a>(RArray&lt;TInt&gt;&amp; aArray)
<a name="l00130"></a>00130         {
<a name="l00131"></a>00131         <span class="keywordflow">for</span>(TInt ix = 0; ix &lt; iArray.Count(); ix++)
<a name="l00132"></a>00132                 {
<a name="l00133"></a>00133                 aArray.Append(iArray[ix]);
<a name="l00134"></a>00134                 }
<a name="l00135"></a>00135         }
<a name="l00136"></a>00136 
<a name="l00140"></a><a class="code" href="class_c_queue.html#a73412173b1f7427ccd1b993acf5fb66c">00140</a> <a class="code" href="class_c_queue.html#a73412173b1f7427ccd1b993acf5fb66c">CQueue::~CQueue</a>()
<a name="l00141"></a>00141         {
<a name="l00142"></a>00142         iMutex.Close();
<a name="l00143"></a>00143         iCondVar.Close();
<a name="l00144"></a>00144         }
</pre></div></div>
<hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
</body>
</html>