1998-06-14 12:11:50 +00:00
|
|
|
\section{\class{wxCondition}}\label{wxcondition}
|
|
|
|
|
1999-11-27 23:15:07 +00:00
|
|
|
wxCondition variables correspond to pthread conditions or to Win32 event
|
|
|
|
objects. They may be used in a multithreaded application to wait until the
|
|
|
|
given condition becomes true which happens when the condition becomes signaled.
|
|
|
|
|
|
|
|
For example, if a worker thread is doing some long task and another thread has
|
2000-07-15 19:51:35 +00:00
|
|
|
to wait until it is finished, the latter thread will wait on the condition
|
1999-11-27 23:15:07 +00:00
|
|
|
object and the worker thread will signal it on exit (this example is not
|
|
|
|
perfect because in this particular case it would be much better to just
|
|
|
|
\helpref{Wait()}{wxthreadwait} for the worker thread, but if there are several
|
|
|
|
worker threads it already makes much more sense).
|
1998-06-14 12:11:50 +00:00
|
|
|
|
2002-02-01 15:43:37 +00:00
|
|
|
Note that a call to \helpref{Signal()}{wxconditionsignal} may happen before the
|
2002-04-02 13:15:16 +00:00
|
|
|
other thread calls \helpref{Wait()}{wxconditionwait} and, just as with the
|
|
|
|
pthread conditions, the signal is then lost and so if you want to be sure to
|
|
|
|
get it you must use a mutex together with the condition variable.
|
2002-02-01 15:43:37 +00:00
|
|
|
|
|
|
|
\wxheading{Example}
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
This example shows how a main thread may launch a worker thread which starts
|
|
|
|
running and then waits until the main thread signals it to continue:
|
2002-02-01 15:43:37 +00:00
|
|
|
|
|
|
|
\begin{verbatim}
|
2002-05-28 17:14:59 +00:00
|
|
|
class MySignallingThread : public wxThread
|
2002-02-01 15:43:37 +00:00
|
|
|
{
|
|
|
|
public:
|
2002-05-28 17:14:59 +00:00
|
|
|
MySignallingThread(wxMutex *mutex, wxCondition *condition)
|
2002-02-01 15:43:37 +00:00
|
|
|
{
|
2002-04-02 13:15:16 +00:00
|
|
|
m_mutex = mutex;
|
2002-02-01 15:43:37 +00:00
|
|
|
m_condition = condition;
|
|
|
|
|
|
|
|
Create();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ExitCode Entry()
|
|
|
|
{
|
|
|
|
... do our job ...
|
|
|
|
|
2002-05-28 17:14:59 +00:00
|
|
|
// tell the other(s) thread(s) that we're about to terminate: we must
|
|
|
|
// lock the mutex first or we might signal the condition before the
|
|
|
|
// waiting threads start waiting on it!
|
|
|
|
wxMutexLocker lock(m_mutex);
|
|
|
|
m_condition.Broadcast(); // same as Signal() here -- one waiter only
|
|
|
|
|
2002-02-01 15:43:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxCondition *m_condition;
|
2002-05-28 17:14:59 +00:00
|
|
|
wxMutex *m_mutex;
|
2002-02-01 15:43:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2002-04-02 13:15:16 +00:00
|
|
|
wxMutex mutex;
|
2002-04-02 15:37:57 +00:00
|
|
|
wxCondition condition(mutex);
|
2002-04-02 13:15:16 +00:00
|
|
|
|
2002-05-28 17:14:59 +00:00
|
|
|
// the mutex should be initially locked
|
|
|
|
mutex.Lock();
|
2002-02-01 15:43:37 +00:00
|
|
|
|
2002-05-28 17:14:59 +00:00
|
|
|
// create and run the thread but notice that it won't be able to
|
|
|
|
// exit (and signal its exit) before we unlock the mutex below
|
|
|
|
MySignallingThread *thread = new MySignallingThread(&mutex, &condition);
|
2002-02-01 15:43:37 +00:00
|
|
|
|
2002-05-28 17:14:59 +00:00
|
|
|
thread->Run();
|
2002-04-02 13:15:16 +00:00
|
|
|
|
2002-05-28 17:14:59 +00:00
|
|
|
// wait for the thread termination: Wait() atomically unlocks the mutex
|
|
|
|
// which allows the thread to continue and starts waiting
|
|
|
|
condition.Wait();
|
2002-02-01 15:43:37 +00:00
|
|
|
|
2002-05-28 17:14:59 +00:00
|
|
|
// now we can exit
|
2002-02-01 15:43:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
\end{verbatim}
|
2000-07-15 19:51:35 +00:00
|
|
|
|
2002-05-28 17:14:59 +00:00
|
|
|
Of course, here it would be much better to simply use a joinable thread and
|
|
|
|
call \helpref{wxThread::Wait}{wxthreadwait} on it, but this example does
|
|
|
|
illustrate the importance of properly locking the mutex when using
|
|
|
|
wxCondition.
|
|
|
|
|
1998-06-14 12:11:50 +00:00
|
|
|
\wxheading{Derived from}
|
|
|
|
|
|
|
|
None.
|
|
|
|
|
1999-02-15 20:41:29 +00:00
|
|
|
\wxheading{Include files}
|
|
|
|
|
|
|
|
<wx/thread.h>
|
|
|
|
|
1998-06-14 12:11:50 +00:00
|
|
|
\wxheading{See also}
|
|
|
|
|
|
|
|
\helpref{wxThread}{wxthread}, \helpref{wxMutex}{wxmutex}
|
|
|
|
|
|
|
|
\latexignore{\rtfignore{\wxheading{Members}}}
|
|
|
|
|
|
|
|
\membersection{wxCondition::wxCondition}\label{wxconditionconstr}
|
|
|
|
|
2002-04-02 15:37:57 +00:00
|
|
|
\func{}{wxCondition}{\param{wxMutex\& }{mutex}}
|
1998-06-14 12:11:50 +00:00
|
|
|
|
2002-04-02 15:37:57 +00:00
|
|
|
Default and only constructor. The {\it mutex} must be locked by the caller
|
|
|
|
before calling \helpref{Wait}{wxconditionwait} function.
|
1998-06-14 12:11:50 +00:00
|
|
|
|
|
|
|
\membersection{wxCondition::\destruct{wxCondition}}
|
|
|
|
|
|
|
|
\func{}{\destruct{wxCondition}}{\void}
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
Destroys the wxCondition object. The destructor is not virtual so this class
|
|
|
|
should not be used polymorphically.
|
1998-06-14 12:11:50 +00:00
|
|
|
|
|
|
|
\membersection{wxCondition::Broadcast}\label{wxconditionbroadcast}
|
|
|
|
|
|
|
|
\func{void}{Broadcast}{\void}
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
Broadcasts to all waiting threads, waking all of them up. Note that this method
|
|
|
|
may be called whether the mutex associated with this condition is locked or
|
|
|
|
not.
|
|
|
|
|
|
|
|
\wxheading{See also}
|
|
|
|
|
|
|
|
\helpref{wxCondition::Signal}{wxconditionsignal}
|
1998-06-14 12:11:50 +00:00
|
|
|
|
|
|
|
\membersection{wxCondition::Signal}\label{wxconditionsignal}
|
|
|
|
|
|
|
|
\func{void}{Signal}{\void}
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
Signals the object waking up at most one thread. If several threads are waiting
|
|
|
|
on the same condition, the exact thread which is woken up is undefined. If no
|
|
|
|
threads are waiting, the signal is lost and the condition would have to be
|
|
|
|
signalled again to wake up any thread which may start waiting on it later.
|
|
|
|
|
|
|
|
Note that this method may be called whether the mutex associated with this
|
|
|
|
condition is locked or not.
|
|
|
|
|
|
|
|
\wxheading{See also}
|
|
|
|
|
|
|
|
\helpref{wxCondition::Broadcast}{wxconditionbroadcast}
|
1998-06-14 12:11:50 +00:00
|
|
|
|
|
|
|
\membersection{wxCondition::Wait}\label{wxconditionwait}
|
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
\func{void}{Wait}{\void}
|
1998-06-14 12:11:50 +00:00
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
Waits until the condition is signalled.
|
1998-06-14 12:11:50 +00:00
|
|
|
|
2000-07-15 19:51:35 +00:00
|
|
|
\func{bool}{Wait}{\param{unsigned long}{ sec}, \param{unsigned long}{ nsec}}
|
1998-06-14 12:11:50 +00:00
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
Waits until the condition is signalled or the timeout has elapsed.
|
|
|
|
|
|
|
|
Note that the mutex associated with this condition {\bf must} be acquired by
|
|
|
|
the thread before calling this method.
|
1998-06-14 12:11:50 +00:00
|
|
|
|
|
|
|
\wxheading{Parameters}
|
|
|
|
|
|
|
|
\docparam{sec}{Timeout in seconds}
|
|
|
|
|
|
|
|
\docparam{nsec}{Timeout nanoseconds component (added to {\it sec}).}
|
|
|
|
|
|
|
|
\wxheading{Return value}
|
|
|
|
|
2002-04-02 13:15:16 +00:00
|
|
|
The second form returns {\tt TRUE} if the condition has been signalled, or
|
|
|
|
{\tt FALSE} if it returned because the timeout has elapsed.
|
|
|
|
|
1998-06-14 12:11:50 +00:00
|
|
|
|