QMutexLocker Class Reference

[ QtCore module]

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

方法

Special Methods


详细描述

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

锁定和解锁 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,那里 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.

例如,此复杂函数锁定 QMutex upon entering the function and unlocks the mutex at all the exit points:

 int complexFunction(int flag)
 {
     mutex.lock();
     int retVal = 0;
     switch (flag) {
     case 0:
     case 1:
         retVal = moreComplexFunction(flag);
         break;
     case 2:
         {
             int status = anotherFunction();
             if (status < 0) {
                 mutex.unlock();
                 return -2;
             }
             retVal = status + flag;
         }
         break;
     default:
         if (flag > 10) {
             mutex.unlock();
             return -1;
         }
         break;
     }
     mutex.unlock();
     return retVal;
 }
			

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:

 int complexFunction(int flag)
 {
     QMutexLocker locker(&mutex);
     int retVal = 0;
     switch (flag) {
     case 0:
     case 1:
         return moreComplexFunction(flag);
     case 2:
         {
             int status = anotherFunction();
             if (status < 0)
                 return -2;
             retVal = status + flag;
         }
         break;
     default:
         if (flag > 10)
             return -1;
         break;
     }
     return retVal;
 }
			

Now, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since locker 是自动变量)。

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 还提供 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:

 class SignalWaiter
 {
 private:
     QMutexLocker locker;
 public:
     SignalWaiter(QMutex *mutex)
         : locker(mutex)
     {
     }
     void waitForSignal()
     {
         ...
         while (!signalled)
             waitCondition.wait(locker.mutex());
         ...
     }
 };
			

方法文档编制

QMutexLocker.__init__ ( self , QMutex   m )

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

另请参阅 QMutex.lock ().

QMutex QMutexLocker.mutex ( self )

Returns a pointer to the mutex that was locked in the 构造函数。

QMutexLocker.relock ( self )

重新锁定被解锁的互斥锁定器。

另请参阅 unlock ().

QMutexLocker.unlock ( self )

解锁此互斥锁定器。可以使用 relock() to lock it again. It does not need to be locked when destroyed.

另请参阅 relock ().

object QMutexLocker.__enter__ ( self )

QMutexLocker.__exit__ ( self , object  type , object  value , object  traceback )