QMutexLocker Class Reference

The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.

  1. #include <QMutexLocker>

Note: All functions in this class are thread-safe.

Detailed Description

The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.

Locking and unlocking a QMutex in complex functions and statements or in exception handling code is error-prone and difficult to debug. QMutexLocker can be used in such situations to ensure that the state of the mutex is always well-defined.

QMutexLocker should be created within a function where a QMutex needs to be locked. The mutex is locked when QMutexLocker is created. You can unlock and relock the mutex with unlock() and relock(). If locked, the mutex will be unlocked when the QMutexLocker is destroyed.

For example, this complex function locks a QMutex upon entering the function and unlocks the mutex at all the exit points:

  1. int complexFunction(int flag)
  2.  {
  3.     mutex.lock();
  4.  
  5.     int retVal = 0;
  6.  
  7.     switch (flag)  {
  8.     case 0:
  9.     case 1:
  10.         mutex.unlock();
  11.         return moreComplexFunction(flag);
  12.     case 2:
  13.          {
  14.             int status = anotherFunction();
  15.             if (status < 0)  {
  16.                 mutex.unlock();
  17.                 return -2;
  18.             }
  19.             retVal = status + flag;
  20.         }
  21.         break;
  22.     default:
  23.         if (flag > 10)  {
  24.             mutex.unlock();
  25.             return -1;
  26.         }
  27.         break;
  28.     }
  29.  
  30.     mutex.unlock();
  31.     return retVal;
  32. }

This example function will get more complicated as it is developed, which increases the likelihood that errors will occur.

Using QMutexLocker greatly simplifies the code, and makes it more readable:

  1. int complexFunction(int flag)
  2.  {
  3.     QMutexLocker locker(&mutex);
  4.  
  5.     int retVal = 0;
  6.  
  7.     switch (flag)  {
  8.     case 0:
  9.     case 1:
  10.         return moreComplexFunction(flag);
  11.     case 2:
  12.          {
  13.             int status = anotherFunction();
  14.             if (status < 0)
  15.                 return -2;
  16.             retVal = status + flag;
  17.         }
  18.         break;
  19.     default:
  20.         if (flag > 10)
  21.             return -1;
  22.         break;
  23.     }
  24.  
  25.     return retVal;
  26. }

Now, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since locker is an auto variable).

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.

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:

  1. class SignalWaiter
  2.  {
  3. private:
  4.     QMutexLocker locker;
  5.  
  6. public:
  7.     SignalWaiter(QMutex *mutex)
  8.         : locker(mutex)
  9.      {
  10.     }
  11.  
  12.     void waitForSignal()
  13.      {
  14.         ...
  15.         while (!signalled)
  16.             waitCondition.wait(locker.mutex());
  17.         ...
  18.     }
  19. };

See also QReadLocker, QWriteLocker, and QMutex.

Public Functions

Toggle details QMutexLocker

QMutexLockerQMutexLocker ( QMutex *mutex ) ( QMutex *mutex )

Constructs a QMutexLocker and locks mutex. The mutex will be unlocked when the QMutexLocker is destroyed. If mutex is zero, QMutexLocker does nothing.

See also QMutex::lock().

Look up this member in the source code.

Toggle details QMutexLocker

~QMutexLocker~QMutexLocker () ()

Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor.

See also QMutex::unlock().

Look up this member in the source code.

Toggle details QMutex * QMutexLocker

mutexmutex () ()const

Returns a pointer to the mutex that was locked in the constructor.

Look up this member in the source code.

Toggle details void QMutexLocker

relockrelock () ()

Relocks an unlocked mutex locker.

See also unlock().

Look up this member in the source code.

Toggle details void QMutexLocker

unlockunlock () ()

Unlocks this mutex locker. You can use relock() to lock it again. It does not need to be locked when destroyed.

See also relock().

Look up this member in the source code.

    Notes provided by the Qt Community

    No notes