QSemaphore Class
The QSemaphore class provides a general counting semaphore. More…
| Header | #include <QSemaphore> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QSemaphore(int n = 0) | |
| void | acquire(int n = 1) |
| bool | tryAcquire(int n, int timeoutMs) |
| bool | tryAcquire(int n = 1) |
| bool | tryAcquire(int n, QDeadlineTimer deadline) |
| bool | try_acquire() |
| bool | try_acquire_for(const std::chrono::duration<Rep, Period> &timeout) |
| bool | try_acquire_until(const std::chrono::time_point<Clock, Duration> &timePoint) |
| void | release(int n = 1) |
| int | available() 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
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.