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.
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.
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.
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.
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:
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.