~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to src/corelib/thread/qwaitcondition.qdoc

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the documentation of the Qt Toolkit.
 
7
**
 
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.
 
16
**
 
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.
 
24
** $QT_END_LICENSE$
 
25
**
 
26
****************************************************************************/
 
27
 
 
28
/*!
 
29
    \class QWaitCondition
 
30
    \inmodule QtCore
 
31
    \brief The QWaitCondition class provides a condition variable for
 
32
    synchronizing threads.
 
33
 
 
34
    \threadsafe
 
35
 
 
36
    \ingroup thread
 
37
 
 
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.
 
43
 
 
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:
 
48
 
 
49
    \snippet code/src_corelib_thread_qwaitcondition_unix.cpp 0
 
50
 
 
51
    Here, the \c keyPressed variable is a global variable of type
 
52
    QWaitCondition.
 
53
 
 
54
    A fourth thread would read key presses and wake the other three
 
55
    threads up every time it receives one, like this:
 
56
 
 
57
    \snippet code/src_corelib_thread_qwaitcondition_unix.cpp 1
 
58
 
 
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:
 
66
 
 
67
    \snippet code/src_corelib_thread_qwaitcondition_unix.cpp 2
 
68
 
 
69
    Here's the code for the fourth thread:
 
70
 
 
71
    \snippet code/src_corelib_thread_qwaitcondition_unix.cpp 3
 
72
 
 
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.
 
76
 
 
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.
 
82
 
 
83
    \sa QMutex, QSemaphore, QThread, {Wait Conditions Example}
 
84
*/
 
85
 
 
86
/*!
 
87
    \fn QWaitCondition::QWaitCondition()
 
88
 
 
89
    Constructs a new wait condition object.
 
90
*/
 
91
 
 
92
/*!
 
93
    \fn QWaitCondition::~QWaitCondition()
 
94
 
 
95
    Destroys the wait condition object.
 
96
*/
 
97
 
 
98
/*!
 
99
    \fn void QWaitCondition::wakeOne()
 
100
 
 
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.
 
104
 
 
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.
 
108
 
 
109
    \sa wakeAll()
 
110
*/
 
111
 
 
112
/*!
 
113
    \fn void QWaitCondition::wakeAll()
 
114
 
 
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.
 
118
 
 
119
    \sa wakeOne()
 
120
*/
 
121
 
 
122
/*!
 
123
    \fn bool QWaitCondition::wait(QMutex *lockedMutex, unsigned long time)
 
124
 
 
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:
 
131
 
 
132
    \list
 
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
 
138
       wait timed out.
 
139
    \endlist
 
140
 
 
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.
 
144
 
 
145
    \sa wakeOne(), wakeAll()
 
146
*/
 
147
 
 
148
/*!
 
149
    \fn bool QWaitCondition::wait(QReadWriteLock *lockedReadWriteLock, unsigned long time)
 
150
    \since 4.4
 
151
 
 
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:
 
159
 
 
160
    \list
 
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
 
166
       wait timed out.
 
167
    \endlist
 
168
 
 
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.
 
172
 
 
173
    \sa wakeOne(), wakeAll()
 
174
*/