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

QSemaphoreReleaser Class

The https:// class provides exception-safe deferral of a QSemaphore::release() call.

This class was introduced in Qt 5.10.

All functions in this class are https://.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QSemaphoreReleaser Class▲

  • Header: QSemaphoreReleaser

  • Since: Qt 5.10

  • qmake: QT += core

  • Group: QSemaphoreReleaser is part of thread

Detailed Description▲

https:// can be used wherever you would otherwise use QSemaphore::release(). Constructing a https:// defers the release() call on the semaphore until the https:// is destroyed (see http://).

You can use this to reliably release a semaphore to avoid dead-lock in the face of exceptions or early returns:

 
Sélectionnez
// ... do something that may throw or return early
sem.release();

If an early return is taken or an exception is thrown before the sem.release() call is reached, the semaphore is not released, possibly preventing the thread waiting in the corresponding sem.acquire() call from ever continuing execution.

When using RAII instead:

 
Sélectionnez
const QSemaphoreReleaser releaser(sem);
// ... do something that may throw or early return
// implicitly calls sem.release() here and at every other return in between

this can no longer happen, because the compiler will make sure that the https:// destructor is always called, and therefore the semaphore is always released.

https:// is move-enabled and can therefore be returned from functions to transfer responsibility for releasing a semaphore out of a function or a scope:

 
Sélectionnez
{ // some scope
    QSemaphoreReleaser releaser; // does nothing
    // ...
    if (someCondition) {
        releaser = QSemaphoreReleaser(sem);
        // ...
    }
    // ...
} // conditionally calls sem.release(), depending on someCondition

A https:// can be canceled by a call to cancel(). A canceled semaphore releaser will no longer call QSemaphore::release() in its destructor.

See Also▲

See also https://

Member Function Documentation▲

 

QSemaphoreReleaser::QSemaphoreReleaser()▲

Default constructor. Creates a https:// that does nothing.

[explicit] QSemaphoreReleaser::QSemaphoreReleaser(int &sem, int n = 1)▲

Constructor. Stores the arguments and calls sem.release(n) in the destructor.

[default] QSemaphoreReleaser::QSemaphoreReleaser(int *sem, int n)▲

Constructor. Stores the arguments and calls sem->release(n) in the destructor.

[default] QSemaphoreReleaser::QSemaphoreReleaser(QSemaphoreReleaser &&other)▲

Move constructor. Takes over responsibility to call QSemaphore::release() from other, which in turn is canceled.

See Also▲

See also cancel()

[default] QSemaphoreReleaser::~QSemaphoreReleaser()▲

Unless canceled, calls QSemaphore::release() with the arguments provided to the constructor, or by the last move assignment.

[default] int QSemaphoreReleaser::operator=(QSemaphoreReleaser &&)▲

Move assignment operator. Takes over responsibility to call QSemaphore::release() from other, which in turn is canceled.

If this semaphore releaser had the responsibility to call some QSemaphore::release() itself, it performs the call before taking over from other.

See Also▲

See also cancel()

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.