1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the documentation of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:FDL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Free Documentation License Usage
18
** Alternatively, this file may be used under the terms of the GNU Free
19
** Documentation License version 1.3 as published by the Free Software
20
** Foundation and appearing in the file included in the packaging of
21
** this file. Please review the following information to ensure
22
** the GNU Free Documentation License version 1.3 requirements
23
** will be met: http://www.gnu.org/copyleft/fdl.html.
26
****************************************************************************/
31
\brief The QWaitCondition class provides a condition variable for
32
synchronizing threads.
38
QWaitCondition allows a thread to tell other threads that some
39
sort of condition has been met. One or many threads can block
40
waiting for a QWaitCondition to set a condition with wakeOne() or
41
wakeAll(). Use wakeOne() to wake one randomly selected thread or
42
wakeAll() to wake them all.
44
For example, let's suppose that we have three tasks that should
45
be performed whenever the user presses a key. Each task could be
46
split into a thread, each of which would have a
47
\l{QThread::run()}{run()} body like this:
49
\snippet code/src_corelib_thread_qwaitcondition_unix.cpp 0
51
Here, the \c keyPressed variable is a global variable of type
54
A fourth thread would read key presses and wake the other three
55
threads up every time it receives one, like this:
57
\snippet code/src_corelib_thread_qwaitcondition_unix.cpp 1
59
The order in which the three threads are woken up is undefined.
60
Also, if some of the threads are still in \c do_something() when
61
the key is pressed, they won't be woken up (since they're not
62
waiting on the condition variable) and so the task will not be
63
performed for that key press. This issue can be solved using a
64
counter and a QMutex to guard it. For example, here's the new
65
code for the worker threads:
67
\snippet code/src_corelib_thread_qwaitcondition_unix.cpp 2
69
Here's the code for the fourth thread:
71
\snippet code/src_corelib_thread_qwaitcondition_unix.cpp 3
73
The mutex is necessary because the results of two threads
74
attempting to change the value of the same variable
75
simultaneously are unpredictable.
77
Wait conditions are a powerful thread synchronization primitive.
78
The \l{threads/waitconditions}{Wait Conditions} example shows how
79
to use QWaitCondition as an alternative to QSemaphore for
80
controlling access to a circular buffer shared by a producer
81
thread and a consumer thread.
83
\sa QMutex, QSemaphore, QThread, {Wait Conditions Example}
87
\fn QWaitCondition::QWaitCondition()
89
Constructs a new wait condition object.
93
\fn QWaitCondition::~QWaitCondition()
95
Destroys the wait condition object.
99
\fn void QWaitCondition::wakeOne()
101
Wakes one thread waiting on the wait condition. The thread that
102
is woken up depends on the operating system's scheduling
103
policies, and cannot be controlled or predicted.
105
If you want to wake up a specific thread, the solution is
106
typically to use different wait conditions and have different
107
threads wait on different conditions.
113
\fn void QWaitCondition::wakeAll()
115
Wakes all threads waiting on the wait condition. The order in
116
which the threads are woken up depends on the operating system's
117
scheduling policies and cannot be controlled or predicted.
123
\fn bool QWaitCondition::wait(QMutex *lockedMutex, unsigned long time)
125
Releases the \a lockedMutex and waits on the wait condition. The
126
\a lockedMutex must be initially locked by the calling thread. If \a
127
lockedMutex is not in a locked state, this function returns
128
immediately. If \a lockedMutex is a recursive mutex, this function
129
returns immediately. The \a lockedMutex will be unlocked, and the
130
calling thread will block until either of these conditions is met:
133
\li Another thread signals it using wakeOne() or wakeAll(). This
134
function will return true in this case.
135
\li \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
136
(the default), then the wait will never timeout (the event
137
must be signalled). This function will return false if the
141
The \a lockedMutex will be returned to the same locked state. This
142
function is provided to allow the atomic transition from the
143
locked state to the wait state.
145
\sa wakeOne(), wakeAll()
149
\fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, unsigned long time)
152
Releases the \a lockedReadWriteLock and waits on the wait
153
condition. The \a lockedReadWriteLock must be initially locked by the
154
calling thread. If \a lockedReadWriteLock is not in a locked state, this
155
function returns immediately. The \a lockedReadWriteLock must not be
156
locked recursively, otherwise this function will not release the
157
lock properly. The \a lockedReadWriteLock will be unlocked, and the
158
calling thread will block until either of these conditions is met:
161
\li Another thread signals it using wakeOne() or wakeAll(). This
162
function will return true in this case.
163
\li \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
164
(the default), then the wait will never timeout (the event
165
must be signalled). This function will return false if the
169
The \a lockedReadWriteLock will be returned to the same locked
170
state. This function is provided to allow the atomic transition
171
from the locked state to the wait state.
173
\sa wakeOne(), wakeAll()