source: trunk/doc/src/qwaitcondition.qdoc@ 109

Last change on this file since 109 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 7.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \class QWaitCondition
44 \brief The QWaitCondition class provides a condition variable for
45 synchronizing threads.
46
47 \threadsafe
48
49 \ingroup thread
50 \ingroup environment
51
52 QWaitCondition allows a thread to tell other threads that some
53 sort of condition has been met. One or many threads can block
54 waiting for a QWaitCondition to set a condition with wakeOne() or
55 wakeAll(). Use wakeOne() to wake one randomly selected condition or
56 wakeAll() to wake them all.
57
58 For example, let's suppose that we have three tasks that should
59 be performed whenever the user presses a key. Each task could be
60 split into a thread, each of which would have a
61 \l{QThread::run()}{run()} body like this:
62
63 \snippet doc/src/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp 0
64
65 Here, the \c keyPressed variable is a global variable of type
66 QWaitCondition.
67
68 A fourth thread would read key presses and wake the other three
69 threads up every time it receives one, like this:
70
71 \snippet doc/src/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp 1
72
73 The order in which the three threads are woken up is undefined.
74 Also, if some of the threads are still in \c do_something() when
75 the key is pressed, they won't be woken up (since they're not
76 waiting on the condition variable) and so the task will not be
77 performed for that key press. This issue can be solved using a
78 counter and a QMutex to guard it. For example, here's the new
79 code for the worker threads:
80
81 \snippet doc/src/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp 2
82
83 Here's the code for the fourth thread:
84
85 \snippet doc/src/snippets/code/src_corelib_thread_qwaitcondition_unix.cpp 3
86
87 The mutex is necessary because the results of two threads
88 attempting to change the value of the same variable
89 simultaneously are unpredictable.
90
91 Wait conditions are a powerful thread synchronization primitive.
92 The \l{threads/waitconditions}{Wait Conditions} example shows how
93 to use QWaitCondition as an alternative to QSemaphore for
94 controlling access to a circular buffer shared by a producer
95 thread and a consumer thread.
96
97 \sa QMutex, QSemaphore, QThread, {Wait Conditions Example}
98*/
99
100/*!
101 \fn QWaitCondition::QWaitCondition()
102
103 Constructs a new wait condition object.
104*/
105
106/*!
107 \fn QWaitCondition::~QWaitCondition()
108
109 Destroys the wait condition object.
110*/
111
112/*!
113 \fn void QWaitCondition::wakeOne()
114
115 Wakes one thread waiting on the wait condition. The thread that
116 is woken up depends on the operating system's scheduling
117 policies, and cannot be controlled or predicted.
118
119 If you want to wake up a specific thread, the solution is
120 typically to use different wait conditions and have different
121 threads wait on different conditions.
122
123 \sa wakeAll()
124*/
125
126/*!
127 \fn void QWaitCondition::wakeAll()
128
129 Wakes all threads waiting on the wait condition. The order in
130 which the threads are woken up depends on the operating system's
131 scheduling policies and cannot be controlled or predicted.
132
133 \sa wakeOne()
134*/
135
136/*!
137 \fn bool QWaitCondition::wait(QMutex *mutex, unsigned long time)
138
139 Releases the locked \a mutex and waits on the wait condition. The
140 \a mutex must be initially locked by the calling thread. If \a
141 mutex is not in a locked state, this function returns
142 immediately. If \a mutex is a recursive mutex, this function
143 returns immediately. The \a mutex will be unlocked, and the
144 calling thread will block until either of these conditions is met:
145
146 \list
147 \o Another thread signals it using wakeOne() or wakeAll(). This
148 function will return true in this case.
149 \o \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
150 (the default), then the wait will never timeout (the event
151 must be signalled). This function will return false if the
152 wait timed out.
153 \endlist
154
155 The mutex will be returned to the same locked state. This
156 function is provided to allow the atomic transition from the
157 locked state to the wait state.
158
159 \sa wakeOne(), wakeAll()
160*/
161
162/*!
163 \fn bool QWaitCondition::wait(QReadWriteLock *readWriteLock, unsigned long time)
164 \since 4.4
165
166 Releases the locked \a readWriteLock and waits on the wait
167 condition. The \a readWriteLock must be initially locked by the
168 calling thread. If \a readWriteLock is not in a locked state, this
169 function returns immediately. The \a readWriteLock must not be
170 locked recursively, otherwise this function will not release the
171 lock properly. The \a readWriteLock will be unlocked, and the
172 calling thread will block until either of these conditions is met:
173
174 \list
175 \o Another thread signals it using wakeOne() or wakeAll(). This
176 function will return true in this case.
177 \o \a time milliseconds has elapsed. If \a time is \c ULONG_MAX
178 (the default), then the wait will never timeout (the event
179 must be signalled). This function will return false if the
180 wait timed out.
181 \endlist
182
183 The \a readWriteLock will be returned to the same locked
184 state. This function is provided to allow the atomic transition
185 from the locked state to the wait state.
186
187 \sa wakeOne(), wakeAll()
188*/
Note: See TracBrowser for help on using the repository browser.