QWaitCondition Class
The QWaitCondition class provides a condition variable for synchronizing threads. More…
| Header | #include <QWaitCondition> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QWaitCondition() | |
| bool | wait(QMutex *mutex, unsigned long timeMs = ULONG_MAX) |
| bool | wait(QMutex *mutex, QDeadlineTimer deadline) |
| bool | wait(QReadWriteLock *lock, unsigned long timeMs = ULONG_MAX) |
| bool | wait(QReadWriteLock *lock, QDeadlineTimer deadline) |
| void | wakeOne() |
| void | wakeAll() |
| void | notify_one() |
| void | notify_all() |
Detailed Description
A QWaitCondition lets a thread sleep until another thread signals that something has changed. The waiting thread passes a locked QMutex to wait(); the mutex is released while the thread sleeps and locked again before wait() returns.
QMutex mutex;
QWaitCondition ready;
bool done = false;
void consumer()
{
QMutexLocker locker(&mutex);
while (!done)
ready.wait(&mutex);
}
void producer()
{
{
QMutexLocker locker(&mutex);
done = true;
}
ready.wakeAll();
}
See also QMutex and QSemaphore.
Differences from Qt
The notify_one() and notify_all() members, named as in the C++ standard library, are additions.
Member Function Documentation
QWaitCondition::QWaitCondition()
Constructs a wait condition.
void QWaitCondition::notify_all()
Equivalent to wakeAll(); provided for compatibility with the C++ standard library naming.
void QWaitCondition::notify_one()
Equivalent to wakeOne(); provided for compatibility with the C++ standard library naming.
bool QWaitCondition::wait(QMutex *mutex, unsigned long timeMs = ULONG_MAX)
Releases mutex and waits on the condition for up to timeMs milliseconds. The mutex must be locked by the calling thread, and it is locked again before this function returns.
Returns true if the thread was woken by wakeOne() or wakeAll(), or false if the time elapsed. The default timeMs, ULONG_MAX, waits indefinitely.
See also wakeOne() and wakeAll().
bool QWaitCondition::wait(QMutex *mutex, QDeadlineTimer deadline)
This is an overloaded function.
Releases mutex and waits until woken or until deadline expires. A deadline that never expires waits indefinitely.
bool QWaitCondition::wait(QReadWriteLock *lock, unsigned long timeMs = ULONG_MAX)
This is an overloaded function.
Releases lock and waits on the condition for up to timeMs milliseconds. The lock must be held by the calling thread, for reading or for writing, and it is locked again in the same mode before this function returns. The default timeMs, ULONG_MAX, waits indefinitely.
bool QWaitCondition::wait(QReadWriteLock *lock, QDeadlineTimer deadline)
This is an overloaded function.
Releases lock and waits until woken or until deadline expires. The lock is locked again, in the mode the calling thread held it, before this function returns.
A recursive lock that the calling thread has locked more than once cannot be released; the function then returns false at once.
void QWaitCondition::wakeAll()
Wakes all threads waiting on the condition.
See also wakeOne() and wait().
void QWaitCondition::wakeOne()
Wakes one thread waiting on the condition. Which thread is woken is unspecified.