📄 cml__threads_8h-source.html
字号:
<a name="l00124"></a>00124 <span class="preprocessor">#endif</span><a name="l00125"></a>00125 <span class="preprocessor"></span>};<a name="l00126"></a>00126 <a name="l00127"></a>00127 <a name="l00128"></a>00128 <span class="comment">/***************************************************************************/</span><span class="comment"></span><a name="l00129"></a>00129 <span class="comment">/**</span><a name="l00130"></a>00130 <span class="comment">This class represents an object that can be used by multiple threads to gain</span><a name="l00131"></a>00131 <span class="comment">safe access to a shared resource. If an attempt is made to lock a mutex that</span><a name="l00132"></a>00132 <span class="comment">is currently locked by another thread, the thread attempting the lock will be</span><a name="l00133"></a>00133 <span class="comment">suspended until the thread holding the lock releases it.</span><a name="l00134"></a>00134 <span class="comment">Mutex objects are not required to allow recursive access.</span><a name="l00135"></a>00135 <span class="comment">*/</span><a name="l00136"></a>00136 <span class="comment">/***************************************************************************/</span><a name="l00137"></a><a class="code" href="classMutex.html">00137</a> <span class="keyword">class </span><a class="code" href="classMutex.html">Mutex</a><a name="l00138"></a>00138 {<a name="l00139"></a>00139 <span class="keyword">public</span>:<span class="comment"></span><a name="l00140"></a>00140 <span class="comment"> /// Create a new mutex object</span><a name="l00141"></a>00141 <span class="comment"></span> <a class="code" href="classMutex.html#a0">Mutex</a>( <span class="keywordtype">void</span> );<a name="l00142"></a>00142 <span class="comment"></span><a name="l00143"></a>00143 <span class="comment"> /// Free any system resources associated with the mutex</span><a name="l00144"></a>00144 <span class="comment"></span> <span class="keyword">virtual</span> <a class="code" href="classMutex.html#a1">~Mutex</a>();<a name="l00145"></a>00145 <span class="comment"></span><a name="l00146"></a>00146 <span class="comment"> /// Lock the mutex. This function causes the calling function to gain </span><a name="l00147"></a>00147 <span class="comment"> /// exclusive access to the mutex object. If some other thread has the</span><a name="l00148"></a>00148 <span class="comment"> /// mutex locked when this method is called, the calling thread will </span><a name="l00149"></a>00149 <span class="comment"> /// block until the mutex is unlocked.</span><a name="l00150"></a>00150 <span class="comment"> /// @return An error object.</span><a name="l00151"></a>00151 <span class="comment"></span> <span class="keyword">const</span> <a class="code" href="classError.html">Error</a> *<a class="code" href="classMutex.html#a2">Lock</a>( <span class="keywordtype">void</span> );<a name="l00152"></a>00152 <span class="comment"></span><a name="l00153"></a>00153 <span class="comment"> /// Unlock the mutex. This function causes the calling thread to give </span><a name="l00154"></a>00154 <span class="comment"> /// up it's lock on the mutex. A task switch may occur before this call</span><a name="l00155"></a>00155 <span class="comment"> /// returns if a high priority task is currently trying to lock the mutex.</span><a name="l00156"></a>00156 <span class="comment"> /// @return An error object.</span><a name="l00157"></a>00157 <span class="comment"></span> <span class="keyword">const</span> <a class="code" href="classError.html">Error</a> *<a class="code" href="classMutex.html#a3">Unlock</a>( <span class="keywordtype">void</span> ); <a name="l00158"></a>00158 <a name="l00159"></a>00159 <span class="keyword">private</span>:<span class="comment"></span><a name="l00160"></a>00160 <span class="comment"> /// Pointer to any system specific data necessary to implement the mutex.</span><a name="l00161"></a>00161 <span class="comment"></span><span class="preprocessor">#ifdef CML_EXTRA_MUTEX_DATA</span><a name="l00162"></a>00162 <span class="preprocessor"></span> <a class="code" href="CML__Utils_8h.html#a2">byte</a> data[ CML_EXTRA_MUTEX_DATA ];<a name="l00163"></a>00163 <span class="preprocessor">#else</span><a name="l00164"></a>00164 <span class="preprocessor"></span> <span class="keywordtype">void</span> *data;<a name="l00165"></a>00165 <span class="preprocessor">#endif</span><a name="l00166"></a>00166 <span class="preprocessor"></span>};<a name="l00167"></a>00167 <a name="l00168"></a>00168 <span class="comment">/***************************************************************************/</span><span class="comment"></span><a name="l00169"></a>00169 <span class="comment">/**</span><a name="l00170"></a>00170 <span class="comment">This is a utility class that locks a mutex in it's constructor, and unlocks </span><a name="l00171"></a>00171 <span class="comment">it in it's destructor. It can be used to ensure that a mutex is properly </span><a name="l00172"></a>00172 <span class="comment">unlocked when a function returns. Just create a temporary MutexLocker object</span><a name="l00173"></a>00173 <span class="comment">and pass it the mutex to lock in it's constructor. The mutex will be automatically</span><a name="l00174"></a>00174 <span class="comment">unlocked when the function returns and the MutexLocker is deleted.</span><a name="l00175"></a>00175 <span class="comment">*/</span><a name="l00176"></a>00176 <span class="comment">/***************************************************************************/</span><a name="l00177"></a><a class="code" href="classMutexLocker.html">00177</a> <span class="keyword">class </span><a class="code" href="classMutexLocker.html">MutexLocker</a><a name="l00178"></a>00178 {<a name="l00179"></a>00179 <a class="code" href="classMutex.html">Mutex</a> *mPtr;<a name="l00180"></a>00180 <span class="keyword">public</span>:<span class="comment"></span><a name="l00181"></a>00181 <span class="comment"> /// Lock the passed mutex</span><a name="l00182"></a><a class="code" href="classMutexLocker.html#a0">00182</a> <span class="comment"></span> <a class="code" href="classMutexLocker.html#a0">MutexLocker</a>( <a class="code" href="classMutex.html">Mutex</a> &m )<a name="l00183"></a>00183 {<a name="l00184"></a>00184 mPtr = &m;<a name="l00185"></a>00185 mPtr-><a class="code" href="classMutex.html#a2">Lock</a>();<a name="l00186"></a>00186 }<a name="l00187"></a>00187 <span class="comment"></span><a name="l00188"></a>00188 <span class="comment"> /// Unlock the mutex</span><a name="l00189"></a><a class="code" href="classMutexLocker.html#a1">00189</a> <span class="comment"></span> <a class="code" href="classMutexLocker.html#a1">~MutexLocker</a>()<a name="l00190"></a>00190 {<a name="l00191"></a>00191 mPtr-><a class="code" href="classMutex.html#a3">Unlock</a>();<a name="l00192"></a>00192 }<a name="l00193"></a>00193 };<a name="l00194"></a>00194 <a name="l00195"></a>00195 <span class="comment">/***************************************************************************/</span><span class="comment"></span><a name="l00196"></a>00196 <span class="comment">/**</span><a name="l00197"></a>00197 <span class="comment">Generic semaphore class. Semaphores can be used to allow multiple threads</span><a name="l00198"></a>00198 <span class="comment">to share a pool of shared resources. Semaphores can be used like mutexes,</span><a name="l00199"></a>00199 <span class="comment">however they also implement timeouts and multiple resource counts.</span><a name="l00200"></a>00200 <span class="comment">*/</span><a name="l00201"></a>00201 <span class="comment">/***************************************************************************/</span><a name="l00202"></a><a class="code" href="classSemaphore.html">00202</a> <span class="keyword">class </span><a class="code" href="classSemaphore.html">Semaphore</a><a name="l00203"></a>00203 {<a name="l00204"></a>00204 <span class="keyword">public</span>:<span class="comment"></span><a name="l00205"></a>00205 <span class="comment"> /// Create a new semaphore object. If a count is passed, then the initial </span><a name="l00206"></a>00206 <span class="comment"> /// semaphore count will be initialized to that value. If no count is passed, </span><a name="l00207"></a>00207 <span class="comment"> /// then a count of zero is used.</span><a name="l00208"></a>00208 <span class="comment"> ///</span><a name="l00209"></a>00209 <span class="comment"> /// @param count The initial count of the semaphore. The semaphore's Get method</span><a name="l00210"></a>00210 <span class="comment"> /// may be called that many times before any thread will block on it.</span><a name="l00211"></a>00211 <span class="comment"></span> <a class="code" href="classSemaphore.html#a0">Semaphore</a>( <a class="code" href="CML__Utils_8h.html#a9">int32</a> count=0 );<a name="l00212"></a>00212 <span class="comment"></span><a name="l00213"></a>00213 <span class="comment"> /// Free any system resources associated with this semaphore. Any threads blocking</span><a name="l00214"></a>00214 <span class="comment"> /// on the semaphore should return from the Get() call with an error indication.</span><a name="l00215"></a>00215 <span class="comment"></span> <span class="keyword">virtual</span> <a class="code" href="classSemaphore.html#a1">~Semaphore</a>();<a name="l00216"></a>00216 <span class="comment"></span><a name="l00217"></a>00217 <span class="comment"> /// Get the semaphore with an optional timeout. An error is returned if the </span><a name="l00218"></a>00218 <span class="comment"> /// timeout expires before the semaphore is acquired.</span><a name="l00219"></a>00219 <span class="comment"> /// @param timeout The timeout in milliseconds. Any negative value will cause</span><a name="l00220"></a>00220 <span class="comment"> /// the thread to wait indefinitely. If a timeout of zero is specified, </span><a name="l00221"></a>00221 <span class="comment"> /// the calling thread will return a timeout error without blocking if </span><a name="l00222"></a>00222 <span class="comment"> /// the semaphore is not available.</span><a name="l00223"></a>00223 <span class="comment"> /// @return An error code indicating success or failure.</span><a name="l00224"></a>00224 <span class="comment"></span> <span class="keyword">const</span> <a class="code" href="classError.html">Error</a> *<a class="code" href="classSemaphore.html#a2">Get</a>( <a class="code" href="CML__Utils_8h.html#a9">int32</a> timeout=-1 );<a name="l00225"></a>00225 <span class="comment"></span><a name="l00226"></a>00226 <span class="comment"> /// Increase the count of the semaphore object. If any threads are pending on </span><a name="l00227"></a>00227 <span class="comment"> /// the object, then the highest priority one will be made eligible to run.</span><a name="l00228"></a>00228 <span class="comment"> /// @return An error object</span><a name="l00229"></a>00229 <span class="comment"></span> <span class="keyword">const</span> <a class="code" href="classError.html">Error</a> *<a class="code" href="classSemaphore.html#a3">Put</a>( <span class="keywordtype">void</span> ); <a name="l00230"></a>00230 <a name="l00231"></a>00231 <span class="keyword">private</span>:<span class="comment"></span><a name="l00232"></a>00232 <span class="comment"> /// Pointer to any system specific data necessary to implement the semaphore.</span><a name="l00233"></a>00233 <span class="comment"></span><span class="preprocessor">#ifdef CML_EXTRA_SEMAPHORE_DATA</span><a name="l00234"></a>00234 <span class="preprocessor"></span> <a class="code" href="CML__Utils_8h.html#a2">byte</a> data[ CML_EXTRA_SEMAPHORE_DATA ];<a name="l00235"></a>00235 <span class="preprocessor">#else</span><a name="l00236"></a>00236 <span class="preprocessor"></span> <span class="keywordtype">void</span> *data;<a name="l00237"></a>00237 <span class="preprocessor">#endif</span><a name="l00238"></a>00238 <span class="preprocessor"></span>};<a name="l00239"></a>00239 <a name="l00240"></a>00240 <a class="code" href="CML__Settings_8h.html#a12">CML_NAMESPACE_END</a>()<a name="l00241"></a>00241 <a name="l00242"></a>00242 #endif<a name="l00243"></a>00243 <a name="l00244"></a>00244 </pre></div><hr><address style="align: right;"><small>Copley Motion Library, Copyright (c) 2002-2003<a href="http://www.copleycontrols.com"><img src="CCC_logo.gif" alt="Copley Controls Corp." align="middle" border=0 ></a></small></address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -