The QMutexLocker class simplifies locking and unlocking QMutexes.
.PP
The purpose of QMutexLocker is to simplify QMutex locking and unlocking. Locking and unlocking a QMutex in complex functions and statements or in exception handling code is error prone and difficult to debug. QMutexLocker should be used in such situations to ensure that the state of the mutex is well defined and always locked and unlocked properly.
.PP
QMutexLocker should be created within a function where a QMutex needs to be locked. The mutex is locked when QMutexLocker is created, and unlocked when QMutexLocker is destroyed.
.PP
For example, this complex function locks a QMutex 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 QMutexLocker greatly simplifies the code, and makes it more readable:
.PP
.nf
.br
int complexFunction( int flag )
.br
{
.br
QMutexLocker 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 QMutexLocker 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
QMutexLocker also provides a mutex() member function that returns the mutex on which the QMutexLocker is operating. This is useful for code that needs access to the mutex, such as QWaitCondition::wait(). For example:
.PP
.nf
.br
class SignalWaiter
.br
{
.br
private:
.br
QMutexLocker locker;
.br
.br
public:
.br
SignalWaiter( QMutex *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 QMutex, QWaitCondition, Environment Classes, and Threading.
Constructs a QMutexLocker and locks \fImutex\fR. The mutex will be unlocked when the QMutexLocker is destroyed. If \fImutex\fR is zero, QMutexLocker does nothing.
.PP
See also QMutex::lock().
.SH "QMutexLocker::~QMutexLocker ()"
Destroys the QMutexLocker and unlocks the mutex which was locked in the constructor.
.PP
See also QMutexLocker::QMutexLocker() and QMutex::unlock().
.SH "QMutex * QMutexLocker::mutex () const"
Returns a pointer to the mutex which was locked in the constructor.
.PP
See also QMutexLocker::QMutexLocker().
.SH "SEE ALSO"
.BR http://doc.trolltech.com/qmutexlocker.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 Qt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using Qt 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 (qmutexlocker.3qt) and the Qt