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.
tqt3/doc/man/man3/tqmutexlocker.3qt

274 lines
6.2 KiB

'\" t
.TH TQMutexLocker 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
TQMutexLocker \- Simplifies locking and unlocking TQMutexes
.SH SYNOPSIS
All the functions in this class are thread-safe when TQt is built with thread support.</p>
.PP
\fC#include <tqmutex.h>\fR
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "\fBTQMutexLocker\fR ( TQMutex * mutex )"
.br
.ti -1c
.BI "\fB~TQMutexLocker\fR ()"
.br
.ti -1c
.BI "TQMutex * \fBmutex\fR () const"
.br
.in -1c
.SH DESCRIPTION
The TQMutexLocker class simplifies locking and unlocking TQMutexes.
.PP
The purpose of TQMutexLocker is to simplify TQMutex locking and 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.
.PP
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.
.PP
For example, this complex function locks a TQMutex upon entering the function and unlocks the mutex at all the exit points:
.PP
.nf
.br
int complexFunction( int flag )
.br
{
.br
mutex.lock();
.br
.br
int return_value = 0;
.br
.br
switch ( flag ) {
.br
case 0:
.br
case 1:
.br
{
.br
mutex.unlock();
.br
return moreComplexFunction( flag );
.br
}
.br
.br
case 2:
.br
{
.br
int status = anotherFunction();
.br
if ( status < 0 ) {
.br
mutex.unlock();
.br
return -2;
.br
}
.br
return_value = status + flag;
.br
break;
.br
}
.br
.br
default:
.br
{
.br
if ( flag > 10 ) {
.br
mutex.unlock();
.br
return -1;
.br
}
.br
break;
.br
}
.br
}
.br
.br
mutex.unlock();
.br
return return_value;
.br
}
.br
.fi
.PP
This example function will get more complicated as it is developed, which increases the likelihood that errors will occur.
.PP
Using TQMutexLocker greatly simplifies the code, and makes it more readable:
.PP
.nf
.br
int complexFunction( int flag )
.br
{
.br
TQMutexLocker locker( &mutex );
.br
.br
int return_value = 0;
.br
.br
switch ( flag ) {
.br
case 0:
.br
case 1:
.br
{
.br
return moreComplexFunction( flag );
.br
}
.br
.br
case 2:
.br
{
.br
int status = anotherFunction();
.br
if ( status < 0 )
.br
return -2;
.br
return_value = status + flag;
.br
break;
.br
}
.br
.br
default:
.br
{
.br
if ( flag > 10 )
.br
return -1;
.br
break;
.br
}
.br
}
.br
.br
return return_value;
.br
}
.br
.fi
.PP
Now, the mutex will always be unlocked when the TQMutexLocker object is destroyed (when the function returns since \fClocker\fR 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.
.PP
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.
.PP
TQMutexLocker also provides a mutex() 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 TQWaitCondition::wait(). For example:
.PP
.nf
.br
class SignalWaiter
.br
{
.br
private:
.br
TQMutexLocker locker;
.br
.br
public:
.br
SignalWaiter( TQMutex *mutex )
.br
: locker( mutex )
.br
{
.br
}
.br
.br
void waitForSignal()
.br
{
.br
...
.br
...
.br
...
.br
.br
while ( ! signalled )
.br
waitcondition.wait( locker.mutex() );
.br
.br
...
.br
...
.br
...
.br
}
.br
};
.br
.fi
.PP
See also TQMutex, TQWaitCondition, Environment Classes, and Threading.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "TQMutexLocker::TQMutexLocker ( TQMutex * mutex )"
Constructs a TQMutexLocker and locks \fImutex\fR. The mutex will be unlocked when the TQMutexLocker is destroyed. If \fImutex\fR is zero, TQMutexLocker does nothing.
.PP
See also TQMutex::lock().
.SH "TQMutexLocker::~TQMutexLocker ()"
Destroys the TQMutexLocker and unlocks the mutex which was locked in the constructor.
.PP
See also TQMutexLocker::TQMutexLocker() and TQMutex::unlock().
.SH "TQMutex * TQMutexLocker::mutex () const"
Returns a pointer to the mutex which was locked in the constructor.
.PP
See also TQMutexLocker::TQMutexLocker().
.SH "SEE ALSO"
.BR http://doc.trolltech.com/tqmutexlocker.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive TQt documentation is provided in HTML format; it is
located at $TQTDIR/doc/html and can be read using TQt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech.
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (tqmutexlocker.3qt) and the Qt
version (3.3.8).