]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/conditn.tex
A little cleanup for this demo
[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
VZ
14Note that a call to \helpref{Signal()}{wxconditionsignal} may happen before the
15other thread calls \helpref{Wait()}{wxconditionwait} but, in marked contrast
16with the pthread conditions, this will still work as the missed signals are
17queued and \helpref{Wait()}{wxconditionwait} simply returns immediately if
18there are ny pending signals.
19
20However, the calls to \helpref{Broadcast()}{wxconditionbroadcast} are {\bf not}
21queued and so it will only wake up the threads already waiting on the
22condition. Accordingly, you will probably want to use a mutex to ensure that
23the thread(s) you want to be waken up have indeed started to wait before
24calling \helpref{Broadcast()}{wxconditionbroadcast}.
25
26\wxheading{Example}
27
28This example shows how a main thread may launch a worker thread and wait until
29it starts running:
30
31\begin{verbatim}
32class MyWaitingThread : public wxThread
33{
34public:
35 MyWaitingThread(wxCondition *condition)
36 {
37 m_condition = condition;
38
39 Create();
40 }
41
42 virtual ExitCode Entry()
43 {
44 // let the main thread know that we started running
45 m_condition->Signal();
46
47 ... do our job ...
48
49 return 0;
50 }
51
52private:
53 wxCondition *m_condition;
54};
55
56int main()
57{
58 wxCondition condition;
59 MyWaitingThread *thread - new MyWaitingThread(&condition);
60
61 thread->Run();
62
63 // wait until the thread really starts running
64 condition.Wait();
65
66 ...
67
68 return 0;
69}
70\end{verbatim}
f6bcfd97 71
eaaa6a06
JS
72\wxheading{Derived from}
73
74None.
75
954b8ae6
JS
76\wxheading{Include files}
77
78<wx/thread.h>
79
eaaa6a06
JS
80\wxheading{See also}
81
82\helpref{wxThread}{wxthread}, \helpref{wxMutex}{wxmutex}
83
84\latexignore{\rtfignore{\wxheading{Members}}}
85
86\membersection{wxCondition::wxCondition}\label{wxconditionconstr}
87
88\func{}{wxCondition}{\void}
89
90Default constructor.
91
92\membersection{wxCondition::\destruct{wxCondition}}
93
94\func{}{\destruct{wxCondition}}{\void}
95
96Destroys the wxCondition object.
97
98\membersection{wxCondition::Broadcast}\label{wxconditionbroadcast}
99
100\func{void}{Broadcast}{\void}
101
102Broadcasts to all waiting objects.
103
104\membersection{wxCondition::Signal}\label{wxconditionsignal}
105
106\func{void}{Signal}{\void}
107
108Signals the object.
109
110\membersection{wxCondition::Wait}\label{wxconditionwait}
111
f6bcfd97 112\func{void}{Wait}{\void}
eaaa6a06
JS
113
114Waits indefinitely.
115
f6bcfd97 116\func{bool}{Wait}{\param{unsigned long}{ sec}, \param{unsigned long}{ nsec}}
eaaa6a06
JS
117
118Waits until a signal is raised or the timeout has elapsed.
119
120\wxheading{Parameters}
121
eaaa6a06
JS
122\docparam{sec}{Timeout in seconds}
123
124\docparam{nsec}{Timeout nanoseconds component (added to {\it sec}).}
125
126\wxheading{Return value}
127
128The second form returns if the signal was raised, or FALSE if there was a timeout.
129