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 to
17 get it you must use a mutex together with the condition variable.
21 This example shows how a main thread may launch a worker thread which starts
22 running and then waits until the main thread signals it to continue:
25 class MyWaitingThread : public wxThread
28 MyWaitingThread(wxMutex *mutex, wxCondition *condition)
31 m_condition = condition;
36 virtual ExitCode Entry()
38 // wait for the signal from the main thread: it is absolutely necessary
39 // to look the mutex before doing it!
41 m_condition->Signal();
50 wxCondition *m_condition;
56 wxCondition condition(mutex);
58 for ( int i =
0; i <
10; i++ )
60 MyWaitingThread *thread = new MyWaitingThread(&mutex, &condition);
65 // wake up one of the threads
68 // wake up all the other ones
69 condition.Broadcast();
71 ... wait until they terminate or do something else ...
77 \wxheading{Derived from
}
81 \wxheading{Include files
}
87 \helpref{wxThread
}{wxthread
},
\helpref{wxMutex
}{wxmutex
}
89 \latexignore{\rtfignore{\wxheading{Members
}}}
91 \membersection{wxCondition::wxCondition
}\label{wxconditionconstr
}
93 \func{}{wxCondition
}{\param{wxMutex\&
}{mutex
}}
95 Default and only constructor. The
{\it mutex
} must be locked by the caller
96 before calling
\helpref{Wait
}{wxconditionwait
} function.
98 \membersection{wxCondition::
\destruct{wxCondition
}}
100 \func{}{\destruct{wxCondition
}}{\void}
102 Destroys the wxCondition object. The destructor is not virtual so this class
103 should not be used polymorphically.
105 \membersection{wxCondition::Broadcast
}\label{wxconditionbroadcast
}
107 \func{void
}{Broadcast
}{\void}
109 Broadcasts to all waiting threads, waking all of them up. Note that this method
110 may be called whether the mutex associated with this condition is locked or
115 \helpref{wxCondition::Signal
}{wxconditionsignal
}
117 \membersection{wxCondition::Signal
}\label{wxconditionsignal
}
119 \func{void
}{Signal
}{\void}
121 Signals the object waking up at most one thread. If several threads are waiting
122 on the same condition, the exact thread which is woken up is undefined. If no
123 threads are waiting, the signal is lost and the condition would have to be
124 signalled again to wake up any thread which may start waiting on it later.
126 Note that this method may be called whether the mutex associated with this
127 condition is locked or not.
131 \helpref{wxCondition::Broadcast
}{wxconditionbroadcast
}
133 \membersection{wxCondition::Wait
}\label{wxconditionwait
}
135 \func{void
}{Wait
}{\void}
137 Waits until the condition is signalled.
139 \func{bool
}{Wait
}{\param{unsigned long
}{ sec
},
\param{unsigned long
}{ nsec
}}
141 Waits until the condition is signalled or the timeout has elapsed.
143 Note that the mutex associated with this condition
{\bf must
} be acquired by
144 the thread before calling this method.
146 \wxheading{Parameters
}
148 \docparam{sec
}{Timeout in seconds
}
150 \docparam{nsec
}{Timeout nanoseconds component (added to
{\it sec
}).
}
152 \wxheading{Return value
}
154 The second form returns
{\tt TRUE
} if the condition has been signalled, or
155 {\tt FALSE
} if it returned because the timeout has elapsed.