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