]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/conditn.tex
added a few missing tree events to the docs
[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}
25class MyWaitingThread : public wxThread
26{
27public:
be809868 28 MyWaitingThread(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 {
be809868
VZ
38 // wait for the signal from the main thread: it is absolutely necessary
39 // to look the mutex before doing it!
40 m_mutex->Lock();
60ce696e 41 m_condition->Signal();
be809868 42 m_mutex->Unlock();
60ce696e
VZ
43
44 ... do our job ...
45
46 return 0;
47 }
48
49private:
50 wxCondition *m_condition;
51};
52
53int main()
54{
be809868 55 wxMutex mutex;
c112e100 56 wxCondition condition(mutex);
be809868
VZ
57
58 for ( int i = 0; i < 10; i++ )
59 {
60 MyWaitingThread *thread = new MyWaitingThread(&mutex, &condition);
60ce696e 61
be809868
VZ
62 thread->Run();
63 }
60ce696e 64
be809868
VZ
65 // wake up one of the threads
66 condition.Signal();
60ce696e 67
be809868
VZ
68 // wake up all the other ones
69 condition.Broadcast();
70
71 ... wait until they terminate or do something else ...
60ce696e
VZ
72
73 return 0;
74}
75\end{verbatim}
f6bcfd97 76
eaaa6a06
JS
77\wxheading{Derived from}
78
79None.
80
954b8ae6
JS
81\wxheading{Include files}
82
83<wx/thread.h>
84
eaaa6a06
JS
85\wxheading{See also}
86
87\helpref{wxThread}{wxthread}, \helpref{wxMutex}{wxmutex}
88
89\latexignore{\rtfignore{\wxheading{Members}}}
90
91\membersection{wxCondition::wxCondition}\label{wxconditionconstr}
92
c112e100 93\func{}{wxCondition}{\param{wxMutex\& }{mutex}}
eaaa6a06 94
c112e100
VZ
95Default and only constructor. The {\it mutex} must be locked by the caller
96before calling \helpref{Wait}{wxconditionwait} function.
eaaa6a06
JS
97
98\membersection{wxCondition::\destruct{wxCondition}}
99
100\func{}{\destruct{wxCondition}}{\void}
101
be809868
VZ
102Destroys the wxCondition object. The destructor is not virtual so this class
103should not be used polymorphically.
eaaa6a06
JS
104
105\membersection{wxCondition::Broadcast}\label{wxconditionbroadcast}
106
107\func{void}{Broadcast}{\void}
108
be809868
VZ
109Broadcasts to all waiting threads, waking all of them up. Note that this method
110may be called whether the mutex associated with this condition is locked or
111not.
112
113\wxheading{See also}
114
115\helpref{wxCondition::Signal}{wxconditionsignal}
eaaa6a06
JS
116
117\membersection{wxCondition::Signal}\label{wxconditionsignal}
118
119\func{void}{Signal}{\void}
120
be809868
VZ
121Signals the object waking up at most one thread. If several threads are waiting
122on the same condition, the exact thread which is woken up is undefined. If no
123threads are waiting, the signal is lost and the condition would have to be
124signalled again to wake up any thread which may start waiting on it later.
125
126Note that this method may be called whether the mutex associated with this
127condition is locked or not.
128
129\wxheading{See also}
130
131\helpref{wxCondition::Broadcast}{wxconditionbroadcast}
eaaa6a06
JS
132
133\membersection{wxCondition::Wait}\label{wxconditionwait}
134
f6bcfd97 135\func{void}{Wait}{\void}
eaaa6a06 136
be809868 137Waits until the condition is signalled.
eaaa6a06 138
f6bcfd97 139\func{bool}{Wait}{\param{unsigned long}{ sec}, \param{unsigned long}{ nsec}}
eaaa6a06 140
be809868
VZ
141Waits until the condition is signalled or the timeout has elapsed.
142
143Note that the mutex associated with this condition {\bf must} be acquired by
144the thread before calling this method.
eaaa6a06
JS
145
146\wxheading{Parameters}
147
eaaa6a06
JS
148\docparam{sec}{Timeout in seconds}
149
150\docparam{nsec}{Timeout nanoseconds component (added to {\it sec}).}
151
152\wxheading{Return value}
153
be809868
VZ
154The second form returns {\tt TRUE} if the condition has been signalled, or
155{\tt FALSE} if it returned because the timeout has elapsed.
156
eaaa6a06 157