]>
Commit | Line | Data |
---|---|---|
eaaa6a06 JS |
1 | \section{\class{wxCondition}}\label{wxcondition} |
2 | ||
9063ea8e VZ |
3 | wxCondition variables correspond to pthread conditions or to Win32 event |
4 | objects. They may be used in a multithreaded application to wait until the | |
5 | given condition becomes true which happens when the condition becomes signaled. | |
6 | ||
7 | For example, if a worker thread is doing some long task and another thread has | |
f6bcfd97 | 8 | to wait until it is finished, the latter thread will wait on the condition |
9063ea8e VZ |
9 | object and the worker thread will signal it on exit (this example is not |
10 | perfect because in this particular case it would be much better to just | |
11 | \helpref{Wait()}{wxthreadwait} for the worker thread, but if there are several | |
12 | worker threads it already makes much more sense). | |
eaaa6a06 | 13 | |
60ce696e | 14 | Note that a call to \helpref{Signal()}{wxconditionsignal} may happen before the |
be809868 | 15 | other thread calls \helpref{Wait()}{wxconditionwait} and, just as with the |
fc4fe308 VZ |
16 | pthread conditions, the signal is then lost and so if you want to be sure that |
17 | you don't miss it you must keep the mutex associated with the condition | |
18 | initially locked and lock it again before calling | |
19 | \helpref{Signal()}{wxconditionsignal}. Of course, this means that this call is | |
20 | going to block until \helpref{Wait()}{wxconditionwait} is called by another | |
21 | thread. | |
60ce696e VZ |
22 | |
23 | \wxheading{Example} | |
24 | ||
be809868 VZ |
25 | This example shows how a main thread may launch a worker thread which starts |
26 | running and then waits until the main thread signals it to continue: | |
60ce696e VZ |
27 | |
28 | \begin{verbatim} | |
d30ff492 | 29 | class MySignallingThread : public wxThread |
60ce696e VZ |
30 | { |
31 | public: | |
d30ff492 | 32 | MySignallingThread(wxMutex *mutex, wxCondition *condition) |
60ce696e | 33 | { |
be809868 | 34 | m_mutex = mutex; |
60ce696e VZ |
35 | m_condition = condition; |
36 | ||
37 | Create(); | |
38 | } | |
39 | ||
40 | virtual ExitCode Entry() | |
41 | { | |
60ce696e VZ |
42 | ... do our job ... |
43 | ||
d30ff492 VZ |
44 | // tell the other(s) thread(s) that we're about to terminate: we must |
45 | // lock the mutex first or we might signal the condition before the | |
46 | // waiting threads start waiting on it! | |
8cc1b7b5 VZ |
47 | wxMutexLocker lock(*m_mutex); |
48 | m_condition->Broadcast(); // same as Signal() here -- one waiter only | |
d30ff492 | 49 | |
60ce696e VZ |
50 | return 0; |
51 | } | |
52 | ||
53 | private: | |
54 | wxCondition *m_condition; | |
d30ff492 | 55 | wxMutex *m_mutex; |
60ce696e VZ |
56 | }; |
57 | ||
58 | int main() | |
59 | { | |
be809868 | 60 | wxMutex mutex; |
c112e100 | 61 | wxCondition condition(mutex); |
be809868 | 62 | |
d30ff492 VZ |
63 | // the mutex should be initially locked |
64 | mutex.Lock(); | |
60ce696e | 65 | |
d30ff492 VZ |
66 | // create and run the thread but notice that it won't be able to |
67 | // exit (and signal its exit) before we unlock the mutex below | |
68 | MySignallingThread *thread = new MySignallingThread(&mutex, &condition); | |
60ce696e | 69 | |
d30ff492 | 70 | thread->Run(); |
be809868 | 71 | |
d30ff492 VZ |
72 | // wait for the thread termination: Wait() atomically unlocks the mutex |
73 | // which allows the thread to continue and starts waiting | |
74 | condition.Wait(); | |
60ce696e | 75 | |
d30ff492 | 76 | // now we can exit |
60ce696e VZ |
77 | return 0; |
78 | } | |
79 | \end{verbatim} | |
f6bcfd97 | 80 | |
d30ff492 VZ |
81 | Of course, here it would be much better to simply use a joinable thread and |
82 | call \helpref{wxThread::Wait}{wxthreadwait} on it, but this example does | |
83 | illustrate the importance of properly locking the mutex when using | |
84 | wxCondition. | |
85 | ||
23efb556 VZ |
86 | \wxheading{Constants} |
87 | ||
88 | The following return codes are returned by wxCondition member functions: | |
89 | ||
90 | \begin{verbatim} | |
91 | enum wxCondError | |
92 | { | |
93 | wxCOND_NO_ERROR = 0, // successful completion | |
94 | wxCOND_INVALID, // object hasn't been initialized successfully | |
95 | wxCOND_TIMEOUT, // WaitTimeout() has timed out | |
96 | wxCOND_MISC_ERROR // some other error | |
97 | }; | |
98 | \end{verbatim} | |
99 | ||
eaaa6a06 JS |
100 | \wxheading{Derived from} |
101 | ||
102 | None. | |
103 | ||
954b8ae6 JS |
104 | \wxheading{Include files} |
105 | ||
106 | <wx/thread.h> | |
107 | ||
a7af285d VZ |
108 | \wxheading{Library} |
109 | ||
110 | \helpref{wxBase}{librarieslist} | |
111 | ||
eaaa6a06 JS |
112 | \wxheading{See also} |
113 | ||
114 | \helpref{wxThread}{wxthread}, \helpref{wxMutex}{wxmutex} | |
115 | ||
116 | \latexignore{\rtfignore{\wxheading{Members}}} | |
117 | ||
f510b7b2 | 118 | \membersection{wxCondition::wxCondition}\label{wxconditionctor} |
eaaa6a06 | 119 | |
c112e100 | 120 | \func{}{wxCondition}{\param{wxMutex\& }{mutex}} |
eaaa6a06 | 121 | |
c112e100 VZ |
122 | Default and only constructor. The {\it mutex} must be locked by the caller |
123 | before calling \helpref{Wait}{wxconditionwait} function. | |
eaaa6a06 | 124 | |
23efb556 | 125 | Use \helpref{IsOk}{wxconditionisok} to check if the object was successfully |
dbd94b75 | 126 | initialized. |
23efb556 | 127 | |
f510b7b2 | 128 | \membersection{wxCondition::\destruct{wxCondition}}\label{wxconditiondtor} |
eaaa6a06 JS |
129 | |
130 | \func{}{\destruct{wxCondition}}{\void} | |
131 | ||
be809868 VZ |
132 | Destroys the wxCondition object. The destructor is not virtual so this class |
133 | should not be used polymorphically. | |
eaaa6a06 JS |
134 | |
135 | \membersection{wxCondition::Broadcast}\label{wxconditionbroadcast} | |
136 | ||
137 | \func{void}{Broadcast}{\void} | |
138 | ||
be809868 VZ |
139 | Broadcasts to all waiting threads, waking all of them up. Note that this method |
140 | may be called whether the mutex associated with this condition is locked or | |
141 | not. | |
142 | ||
143 | \wxheading{See also} | |
144 | ||
145 | \helpref{wxCondition::Signal}{wxconditionsignal} | |
eaaa6a06 | 146 | |
23efb556 VZ |
147 | \membersection{wxCondition::IsOk}\label{wxconditionisok} |
148 | ||
149 | \constfunc{bool}{IsOk}{\void} | |
150 | ||
151 | Returns {\tt true} if the object had been initialized successfully, {\tt false} | |
43e8916f | 152 | if an error occurred. |
23efb556 | 153 | |
eaaa6a06 JS |
154 | \membersection{wxCondition::Signal}\label{wxconditionsignal} |
155 | ||
156 | \func{void}{Signal}{\void} | |
157 | ||
be809868 VZ |
158 | Signals the object waking up at most one thread. If several threads are waiting |
159 | on the same condition, the exact thread which is woken up is undefined. If no | |
160 | threads are waiting, the signal is lost and the condition would have to be | |
161 | signalled again to wake up any thread which may start waiting on it later. | |
162 | ||
163 | Note that this method may be called whether the mutex associated with this | |
164 | condition is locked or not. | |
165 | ||
166 | \wxheading{See also} | |
167 | ||
168 | \helpref{wxCondition::Broadcast}{wxconditionbroadcast} | |
eaaa6a06 JS |
169 | |
170 | \membersection{wxCondition::Wait}\label{wxconditionwait} | |
171 | ||
23efb556 | 172 | \func{wxCondError}{Wait}{\void} |
eaaa6a06 | 173 | |
be809868 | 174 | Waits until the condition is signalled. |
eaaa6a06 | 175 | |
23efb556 VZ |
176 | This method atomically releases the lock on the mutex associated with this |
177 | condition (this is why it must be locked prior to calling Wait) and puts the | |
178 | thread to sleep until \helpref{Signal}{wxconditionsignal} or | |
ddca12cf VZ |
179 | \helpref{Broadcast}{wxconditionbroadcast} is called. It then locks the mutex |
180 | again and returns. | |
23efb556 VZ |
181 | |
182 | Note that even if \helpref{Signal}{wxconditionsignal} had been called before | |
183 | Wait without waking up any thread, the thread would still wait for another one | |
184 | and so it is important to ensure that the condition will be signalled after | |
185 | Wait or the thread may sleep forever. | |
186 | ||
187 | \wxheading{Return value} | |
188 | ||
43e8916f | 189 | Returns {\tt wxCOND\_NO\_ERROR} on success, another value if an error occurred. |
23efb556 VZ |
190 | |
191 | \wxheading{See also} | |
192 | ||
193 | \helpref{WaitTimeout}{wxconditionwaittimeout} | |
194 | ||
195 | ||
196 | \membersection{wxCondition::WaitTimeout}\label{wxconditionwaittimeout} | |
197 | ||
0f353563 | 198 | \func{wxCondError}{WaitTimeout}{\param{unsigned long}{ milliseconds}} |
eaaa6a06 | 199 | |
be809868 VZ |
200 | Waits until the condition is signalled or the timeout has elapsed. |
201 | ||
23efb556 VZ |
202 | This method is identical to \helpref{Wait}{wxconditionwait} except that it |
203 | returns, with the return code of {\tt wxCOND\_TIMEOUT} as soon as the given | |
204 | timeout expires. | |
eaaa6a06 JS |
205 | |
206 | \wxheading{Parameters} | |
207 | ||
23efb556 | 208 | \docparam{milliseconds}{Timeout in milliseconds} |
eaaa6a06 JS |
209 | |
210 | \wxheading{Return value} | |
211 | ||
23efb556 | 212 | Returns {\tt wxCOND\_NO\_ERROR} if the condition was signalled, |
dbd94b75 | 213 | {\tt wxCOND\_TIMEOUT} if the timeout elapsed before this happened or another |
23efb556 | 214 | error code from wxCondError enum. |
eaaa6a06 | 215 |