1 \section{\class{wxCondition
}}\label{wxcondition
}
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.
7 For example, if a worker thread is doing some long task and another thread has
8 to wait until it is finished, the latter thread will wait on the condition
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).
14 Note that a call to
\helpref{Signal()
}{wxconditionsignal
} may happen before the
15 other thread calls
\helpref{Wait()
}{wxconditionwait
} and, just as with the
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
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:
29 class MySignallingThread : public wxThread
32 MySignallingThread(wxMutex *mutex, wxCondition *condition)
35 m_condition = condition;
40 virtual ExitCode Entry()
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!
47 wxMutexLocker lock(m_mutex);
48 m_condition.Broadcast(); // same as Signal() here -- one waiter only
54 wxCondition *m_condition;
61 wxCondition condition(mutex);
63 // the mutex should be initially locked
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);
72 // wait for the thread termination: Wait() atomically unlocks the mutex
73 // which allows the thread to continue and starts waiting
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
88 The following return codes are returned by wxCondition member functions:
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
100 \wxheading{Derived from
}
104 \wxheading{Include files
}
110 \helpref{wxThread
}{wxthread
},
\helpref{wxMutex
}{wxmutex
}
112 \latexignore{\rtfignore{\wxheading{Members
}}}
114 \membersection{wxCondition::wxCondition
}\label{wxconditionctor
}
116 \func{}{wxCondition
}{\param{wxMutex\&
}{mutex
}}
118 Default and only constructor. The
{\it mutex
} must be locked by the caller
119 before calling
\helpref{Wait
}{wxconditionwait
} function.
121 Use
\helpref{IsOk
}{wxconditionisok
} to check if the object was successfully
124 \membersection{wxCondition::
\destruct{wxCondition
}}\label{wxconditiondtor
}
126 \func{}{\destruct{wxCondition
}}{\void}
128 Destroys the wxCondition object. The destructor is not virtual so this class
129 should not be used polymorphically.
131 \membersection{wxCondition::Broadcast
}\label{wxconditionbroadcast
}
133 \func{void
}{Broadcast
}{\void}
135 Broadcasts to all waiting threads, waking all of them up. Note that this method
136 may be called whether the mutex associated with this condition is locked or
141 \helpref{wxCondition::Signal
}{wxconditionsignal
}
143 \membersection{wxCondition::IsOk
}\label{wxconditionisok
}
145 \constfunc{bool
}{IsOk
}{\void}
147 Returns
{\tt true
} if the object had been initialized successfully,
{\tt false
}
148 if an error occurred.
150 \membersection{wxCondition::Signal
}\label{wxconditionsignal
}
152 \func{void
}{Signal
}{\void}
154 Signals the object waking up at most one thread. If several threads are waiting
155 on the same condition, the exact thread which is woken up is undefined. If no
156 threads are waiting, the signal is lost and the condition would have to be
157 signalled again to wake up any thread which may start waiting on it later.
159 Note that this method may be called whether the mutex associated with this
160 condition is locked or not.
164 \helpref{wxCondition::Broadcast
}{wxconditionbroadcast
}
166 \membersection{wxCondition::Wait
}\label{wxconditionwait
}
168 \func{wxCondError
}{Wait
}{\void}
170 Waits until the condition is signalled.
172 This method atomically releases the lock on the mutex associated with this
173 condition (this is why it must be locked prior to calling Wait) and puts the
174 thread to sleep until
\helpref{Signal
}{wxconditionsignal
} or
175 \helpref{Broadcast
}{wxconditionbroadcast
} is called.
177 Note that even if
\helpref{Signal
}{wxconditionsignal
} had been called before
178 Wait without waking up any thread, the thread would still wait for another one
179 and so it is important to ensure that the condition will be signalled after
180 Wait or the thread may sleep forever.
182 \wxheading{Return value
}
184 Returns
{\tt wxCOND
\_NO\_ERROR} on success, another value if an error occurred.
188 \helpref{WaitTimeout
}{wxconditionwaittimeout
}
191 \membersection{wxCondition::WaitTimeout
}\label{wxconditionwaittimeout
}
193 \func{wxCondError
}{WaitTimeout
}{\param{unsigned long
}{ milliseconds
}}
195 Waits until the condition is signalled or the timeout has elapsed.
197 This method is identical to
\helpref{Wait
}{wxconditionwait
} except that it
198 returns, with the return code of
{\tt wxCOND
\_TIMEOUT} as soon as the given
201 \wxheading{Parameters
}
203 \docparam{milliseconds
}{Timeout in milliseconds
}
205 \wxheading{Return value
}
207 Returns
{\tt wxCOND
\_NO\_ERROR} if the condition was signalled,
208 {\tt wxCOND
\_TIMEOUT} if the timeout elapsed before this happened or another
209 error code from wxCondError enum.