IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QMutexLocker Class

The https:// class is a convenience class that simplifies locking and unlocking mutexes.

All functions in this class are thread-safe.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QMutexLocker Class▲

  • Header: QMutexLocker

  • CMake:

    find_package(Qt6 REQUIRED COMPONENTS Core)

    target_link_libraries(mytarget PRIVATE Qt6::Core)

  • qmake: QT += core

  • Group: QMutexLocker is part of thread

Detailed Description▲

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

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

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

 
Sélectionnez
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 https:// greatly simplifies the code, and makes it more readable:

 
Sélectionnez
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 https:// 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.

https:// also provides a mutex() member function that returns the mutex on which the https:// is operating. This is useful for code that needs access to the mutex, such as https://(). For example:

 
Sélectionnez
class SignalWaiter
{
private:
    QMutexLocker<QMutex> locker;

public:
    SignalWaiter(QMutex *mutex)
        : locker(mutex)
    {
    }

    void waitForSignal()
    {
        ...
        while (!signalled)
            waitCondition.wait(locker.mutex());
        ...
    }
};

See Also▲

See also https://, https://, https://

Member Function Documentation▲

 

[explicit] QMutexLocker::QMutexLocker(Mutex *mutex)▲

Constructs a https:// and locks mutex. The mutex will be unlocked when the https:// is destroyed. If mutex is nullptr, https:// does nothing.

See Also▲

See also https://()

QMutexLocker::~QMutexLocker()▲

Destroys the https:// and unlocks the mutex that was locked in the constructor.

See Also▲

See also https://()

Mutex *QMutexLocker::mutex() const▲

Returns the mutex on which the https:// is operating.

void QMutexLocker::relock()▲

Relocks an unlocked mutex locker.

See Also▲

See also https://()

void QMutexLocker::unlock()▲

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

See Also▲

See also https://()

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.