📄 qmutexlocker.3qt
字号:
'\" t.TH QMutexLocker 3qt "9 December 2002" "Trolltech AS" \" -*- nroff -*-.\" Copyright 1992-2001 Trolltech AS. All rights reserved. See the.\" license file included in the distribution for a complete license.\" statement..\".ad l.nh.SH NAMEQMutexLocker \- Simplifies locking and unlocking QMutexes.SH SYNOPSISAll the functions in this class are thread-safe when Qt is built with thread support.</p>.PP\fC#include <qmutex.h>\fR.PP.SS "Public Members".in +1c.ti -1c.BI "\fBQMutexLocker\fR ( QMutex * mutex )".br.ti -1c.BI "\fB~QMutexLocker\fR ()".br.ti -1c.BI "QMutex * \fBmutex\fR () const".br.in -1c.SH DESCRIPTIONThe QMutexLocker class simplifies locking and unlocking QMutexes..PPThe 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..PPQMutexLocker 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..PPFor 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.PPThis example function will get more complicated as it is developed, which increases the likelihood that errors will occur..PPUsing 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.PPNow, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since \fClocker\fR is an auto variable)..PPThe 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..PPQMutexLocker 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.PPSee also QMutex, QWaitCondition, Environment Classes, and Threading..SH MEMBER FUNCTION DOCUMENTATION.SH "QMutexLocker::QMutexLocker ( QMutex * mutex )"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..PPSee also QMutex::lock()..SH "QMutexLocker::~QMutexLocker ()"Destroys the QMutexLocker and unlocks the mutex which was locked in the constructor..PPSee also QMutexLocker::QMutexLocker() and QMutex::unlock()..SH "QMutex * QMutexLocker::mutex () const"Returns a pointer to the mutex which was locked in the constructor..PPSee also QMutexLocker::QMutexLocker()..SH "SEE ALSO".BR http://doc.trolltech.com/qmutexlocker.html.BR http://www.trolltech.com/faq/tech.html.SH COPYRIGHTCopyright 1992-2001 Trolltech AS, http://www.trolltech.com. See thelicense file included in the distribution for a complete licensestatement..SH AUTHORGenerated automatically from the source code..SH BUGSIf 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..PThe definitive Qt documentation is provided in HTML format; it islocated at $QTDIR/doc/html and can be read using Qt Assistant or witha web browser. This man page is provided as a convenience for thoseusers who prefer man pages, although this format is not officiallysupported by Trolltech. .PIf 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 Qtversion (3.1.1).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -