You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
220 lines
7.2 KiB
220 lines
7.2 KiB
13 years ago
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||
|
<title>TQMutexLocker Class</title>
|
||
|
<style type="text/css"><!--
|
||
|
fn { margin-left: 1cm; text-indent: -1cm; }
|
||
|
a:link { color: #004faf; text-decoration: none }
|
||
|
a:visited { color: #672967; text-decoration: none }
|
||
|
body { background: #ffffff; color: black; }
|
||
|
--></style>
|
||
|
</head>
|
||
|
<body>
|
||
|
|
||
|
<table border="0" cellpadding="0" cellspacing="0" width="100%">
|
||
|
<tr bgcolor="#E5E5E5">
|
||
|
<td valign=center>
|
||
|
<a href="index.html">
|
||
|
<font color="#004faf">Home</font></a>
|
||
|
| <a href="classes.html">
|
||
|
<font color="#004faf">All Classes</font></a>
|
||
|
| <a href="mainclasses.html">
|
||
|
<font color="#004faf">Main Classes</font></a>
|
||
|
| <a href="annotated.html">
|
||
|
<font color="#004faf">Annotated</font></a>
|
||
|
| <a href="groups.html">
|
||
|
<font color="#004faf">Grouped Classes</font></a>
|
||
|
| <a href="functions.html">
|
||
|
<font color="#004faf">Functions</font></a>
|
||
|
</td>
|
||
|
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>TQMutexLocker Class Reference</h1>
|
||
|
|
||
|
<p>The TQMutexLocker class simplifies locking and unlocking TQMutexes.
|
||
|
<a href="#details">More...</a>
|
||
|
<p>All the functions in this class are <a href="threads.html#threadsafe">thread-safe</a> when TQt is built with thread support.</p>
|
||
5 months ago
|
<p><tt>#include <<a href="tqmutex-h.html">tqmutex.h</a>></tt>
|
||
|
<p><a href="tqmutexlocker-members.html">List of all member functions.</a>
|
||
13 years ago
|
<h2>Public Members</h2>
|
||
|
<ul>
|
||
|
<li class=fn><a href="#TQMutexLocker"><b>TQMutexLocker</b></a> ( TQMutex * mutex )</li>
|
||
|
<li class=fn><a href="#~TQMutexLocker"><b>~TQMutexLocker</b></a> ()</li>
|
||
|
<li class=fn>TQMutex * <a href="#mutex"><b>mutex</b></a> () const</li>
|
||
|
</ul>
|
||
|
<hr><a name="details"></a><h2>Detailed Description</h2>
|
||
|
|
||
|
|
||
|
The TQMutexLocker class simplifies locking and unlocking TQMutexes.
|
||
|
<p>
|
||
|
<p>
|
||
|
|
||
5 months ago
|
<p> The purpose of TQMutexLocker is to simplify <a href="tqmutex.html">TQMutex</a> locking and
|
||
13 years ago
|
unlocking. Locking and unlocking a TQMutex in complex functions and
|
||
|
statements or in exception handling code is error prone and
|
||
|
difficult to debug. TQMutexLocker should be used in such situations
|
||
|
to ensure that the state of the mutex is well defined and always
|
||
|
locked and unlocked properly.
|
||
|
<p> TQMutexLocker should be created within a function where a TQMutex
|
||
|
needs to be locked. The mutex is locked when TQMutexLocker is
|
||
|
created, and unlocked when TQMutexLocker is destroyed.
|
||
|
<p> For example, this complex function locks a TQMutex upon entering
|
||
|
the function and unlocks the mutex at all the exit points:
|
||
|
<p> <pre>
|
||
|
int complexFunction( int flag )
|
||
|
{
|
||
|
mutex.lock();
|
||
|
|
||
|
int return_value = 0;
|
||
|
|
||
|
switch ( flag ) {
|
||
|
case 0:
|
||
|
case 1:
|
||
|
{
|
||
|
mutex.unlock();
|
||
|
return moreComplexFunction( flag );
|
||
|
}
|
||
|
|
||
|
case 2:
|
||
|
{
|
||
|
int status = anotherFunction();
|
||
|
if ( status < 0 ) {
|
||
|
mutex.unlock();
|
||
|
return -2;
|
||
|
}
|
||
|
return_value = status + flag;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
default:
|
||
|
{
|
||
|
if ( flag > 10 ) {
|
||
|
mutex.unlock();
|
||
|
return -1;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mutex.unlock();
|
||
|
return return_value;
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<p> This example function will get more complicated as it is
|
||
|
developed, which increases the likelihood that errors will occur.
|
||
|
<p> Using TQMutexLocker greatly simplifies the code, and makes it more
|
||
|
readable:
|
||
|
<p> <pre>
|
||
|
int complexFunction( int flag )
|
||
|
{
|
||
|
TQMutexLocker locker( &mutex );
|
||
|
|
||
|
int return_value = 0;
|
||
|
|
||
|
switch ( flag ) {
|
||
|
case 0:
|
||
|
case 1:
|
||
|
{
|
||
|
return moreComplexFunction( flag );
|
||
|
}
|
||
|
|
||
|
case 2:
|
||
|
{
|
||
|
int status = anotherFunction();
|
||
|
if ( status < 0 )
|
||
|
return -2;
|
||
|
return_value = status + flag;
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
default:
|
||
|
{
|
||
|
if ( flag > 10 )
|
||
|
return -1;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return return_value;
|
||
|
}
|
||
|
</pre>
|
||
|
|
||
|
<p> Now, the mutex will always be unlocked when the TQMutexLocker
|
||
|
object is destroyed (when the function returns since <tt>locker</tt> is
|
||
|
an auto variable). Note that the mutex will be unlocked after
|
||
|
the call to moreComplexFunction() in this example, avoiding
|
||
|
possible bugs caused by unlocking the mutex too early, as in
|
||
|
the first example.
|
||
|
<p> The same principle applies to code that throws and catches
|
||
|
exceptions. An exception that is not caught in the function that
|
||
|
has locked the mutex has no way of unlocking the mutex before the
|
||
|
exception is passed up the stack to the calling function.
|
||
|
<p> TQMutexLocker also provides a <a href="#mutex">mutex</a>() member function that returns
|
||
|
the mutex on which the TQMutexLocker is operating. This is useful
|
||
|
for code that needs access to the mutex, such as
|
||
5 months ago
|
<a href="tqwaitcondition.html#wait">TQWaitCondition::wait</a>(). For example:
|
||
13 years ago
|
<p> <pre>
|
||
|
class SignalWaiter
|
||
|
{
|
||
|
private:
|
||
|
TQMutexLocker locker;
|
||
|
|
||
|
public:
|
||
5 months ago
|
SignalWaiter( <a href="tqmutex.html">TQMutex</a> *mutex )
|
||
13 years ago
|
: locker( mutex )
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void waitForSignal()
|
||
|
{
|
||
|
...
|
||
|
...
|
||
|
...
|
||
|
|
||
|
while ( ! signalled )
|
||
|
waitcondition.wait( locker.<a href="#mutex">mutex</a>() );
|
||
|
|
||
|
...
|
||
|
...
|
||
|
...
|
||
|
}
|
||
|
};
|
||
|
</pre>
|
||
|
|
||
5 months ago
|
<p> <p>See also <a href="tqmutex.html">TQMutex</a>, <a href="tqwaitcondition.html">TQWaitCondition</a>, <a href="environment.html">Environment Classes</a>, and <a href="thread.html">Threading</a>.
|
||
13 years ago
|
|
||
|
<hr><h2>Member Function Documentation</h2>
|
||
5 months ago
|
<h3 class=fn><a name="TQMutexLocker"></a>TQMutexLocker::TQMutexLocker ( <a href="tqmutex.html">TQMutex</a> * mutex )
|
||
13 years ago
|
</h3>
|
||
|
|
||
|
<p> Constructs a TQMutexLocker and locks <em>mutex</em>. The mutex will be
|
||
|
unlocked when the TQMutexLocker is destroyed. If <em>mutex</em> is zero,
|
||
|
TQMutexLocker does nothing.
|
||
5 months ago
|
<p> <p>See also <a href="tqmutex.html#lock">TQMutex::lock</a>().
|
||
13 years ago
|
|
||
|
<h3 class=fn><a name="~TQMutexLocker"></a>TQMutexLocker::~TQMutexLocker ()
|
||
|
</h3>
|
||
|
|
||
|
<p> Destroys the TQMutexLocker and unlocks the mutex which was locked
|
||
|
in the constructor.
|
||
5 months ago
|
<p> <p>See also <a href="#TQMutexLocker">TQMutexLocker::TQMutexLocker</a>() and <a href="tqmutex.html#unlock">TQMutex::unlock</a>().
|
||
13 years ago
|
|
||
5 months ago
|
<h3 class=fn><a href="tqmutex.html">TQMutex</a> * <a name="mutex"></a>TQMutexLocker::mutex () const
|
||
13 years ago
|
</h3>
|
||
|
|
||
|
<p> Returns a pointer to the mutex which was locked in the
|
||
|
constructor.
|
||
|
<p> <p>See also <a href="#TQMutexLocker">TQMutexLocker::TQMutexLocker</a>().
|
||
|
|
||
|
<!-- eof -->
|
||
|
<hr><p>
|
||
|
This file is part of the <a href="index.html">TQt toolkit</a>.
|
||
|
Copyright © 1995-2007
|
||
|
<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
|
||
|
<table width=100% cellspacing=0 border=0><tr>
|
||
|
<td>Copyright © 2007
|
||
|
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
||
|
<td align=right><div align=right>TQt 3.3.8</div>
|
||
|
</table></div></address></body>
|
||
|
</html>
|