6.0.0-rc4Source

QSemaphore Class

The QSemaphore class provides a general counting semaphore. More…

Header#include <QSemaphore>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QSemaphore(int n = 0)
voidacquire(int n = 1)
booltryAcquire(int n, int timeoutMs)
booltryAcquire(int n = 1)
booltryAcquire(int n, QDeadlineTimer deadline)
booltry_acquire()
booltry_acquire_for(const std::chrono::duration<Rep, Period> &timeout)
booltry_acquire_until(const std::chrono::time_point<Clock, Duration> &timePoint)
voidrelease(int n = 1)
intavailable() const

Detailed Description

A semaphore guards a number of identical resources. acquire() takes resources, blocking until enough are available, and release() gives them back.

QSemaphore freeSlots(8);

void useSlot()
{
    freeSlots.acquire();
    // work with one slot
    freeSlots.release();
}

See also QSemaphoreReleaser, QMutex and QWaitCondition.

Differences from Qt

The semaphore is built on std::mutex and std::condition_variable, and release() wakes every waiting thread.

Member Function Documentation

[explicit] QSemaphore::QSemaphore(int n = 0)

Constructs a semaphore with n resources available.

void QSemaphore::acquire(int n = 1)

Acquires n resources, blocking until that many are available.

See also release(), tryAcquire() and available().

int QSemaphore::available() const

Returns the number of resources currently available. The value may change as soon as it is returned.

See also acquire() and release().

void QSemaphore::release(int n = 1)

Releases n resources, waking threads that are waiting to acquire them. The count may be raised above the value given to the constructor.

See also acquire() and available().

bool QSemaphore::try_acquire()

Tries to acquire one resource without waiting. Returns true on success. This is provided for compatibility with std::counting_semaphore.

This function was introduced in 6.3.

See also tryAcquire().

template <class Rep, class Period>bool QSemaphore::try_acquire_for(const std::chrono::duration<Rep, Period> &timeout)

Tries to acquire one resource, waiting for at most timeout. Returns true on success. This is provided for compatibility with std::counting_semaphore.

This function was introduced in 6.3.

See also tryAcquire().

template <class Clock, class Duration>bool QSemaphore::try_acquire_until(const std::chrono::time_point<Clock, Duration> &timePoint)

Tries to acquire one resource, waiting until timePoint at most. Returns true on success. This is provided for compatibility with std::counting_semaphore.

This function was introduced in 6.3.

See also tryAcquire().

bool QSemaphore::tryAcquire(int n, int timeoutMs)

Tries to acquire n resources, waiting up to timeoutMs milliseconds for them to become available. Returns true if the resources were acquired; otherwise returns false and acquires nothing. If timeoutMs is 0, the call returns immediately; if it is negative, the call waits like acquire().

See also acquire() and release().

bool QSemaphore::tryAcquire(int n = 1)

This is an overloaded function.

Tries to acquire n resources without waiting. Returns true if they were acquired; otherwise returns false.

bool QSemaphore::tryAcquire(int n, QDeadlineTimer deadline)

This is an overloaded function.

Tries to acquire n resources, waiting until deadline expires. Returns true if the resources were acquired; otherwise returns false and acquires nothing.

This function was introduced in 6.6.