1 \section{\class{wxThread
}}\label{wxthread
}
3 A thread is basically a path of execution through a program. Threads are also
4 sometimes called
{\it light-weight processes
}, but the fundamental difference
5 between threads and processes is that memory spaces of different processes are
6 separated while all threads share the same address space. While it makes it
7 much easier to share common data between several threads, it also makes much
8 easier to shoot oneself in the foot, so careful use of synchronization objects
9 such as
\helpref{mutexes
}{wxmutex
} and/or
\helpref{critical sections
}{wxcriticalsection
} is recommended.
11 \wxheading{Derived from
}
15 \wxheading{Include files
}
21 \helpref{wxMutex
}{wxmutex
},
\helpref{wxCondition
}{wxcondition
},
\helpref{wxCriticalSection
}{wxcriticalsection
}
23 \latexignore{\rtfignore{\wxheading{Members
}}}
25 \membersection{wxThread::wxThread
}\label{wxthreadctor
}
27 \func{}{wxThread
}{\void}
29 Constructor creates a new detached (default) or joinable C++ thread object. It
30 does not create (or starts execution of) the real thread - for this you should
31 use
\helpref{Create
}{wxthreadcreate
} and
\helpref{Run
}{wxthreadrun
} methods.
33 \membersection{wxThread::
\destruct{wxThread
}}
35 \func{}{\destruct{wxThread
}}{\void}
37 Destructor frees the ressources associated with the thread. Notice that you
38 should never delete a detached thread - you may only call
39 \helpref{Delete
}{wxthreaddelete
} on it or wait until it terminates (and auto
40 destructs) itself. Because the detached threads delete themselves, they can
41 only be allocated on the heap.
43 The joinable threads, however, may and should be deleted explicitly and
44 \helpref{Delete
}{wxthreaddelete
} and
\helpref{Kill
}{wxthreadkill
} functions
45 will not delete the C++ thread object. It is also safe to allocate them on
48 \membersection{wxThread::Create
}\label{wxthreadcreate
}
50 \func{wxThreadError
}{Create
}{\void}
52 Creates a new thread. The thread object is created in the suspended state, you
53 should call
\helpref{Run
}{wxthreadrun
} to start running it.
55 \wxheading{Return value
}
60 \begin{twocollist
}\itemsep=
0pt
61 \twocolitem{{\bf wxTHREAD
\_NO\_ERROR}}{There was no error.
}
62 \twocolitem{{\bf wxTHREAD
\_NO\_RESOURCE}}{There were insufficient resources to create a new thread.
}
63 \twocolitem{{\bf wxTHREAD
\_RUNNING}}{The thread is already running.
}
66 \membersection{wxThread::Delete
}\label{wxthreaddelete
}
68 \func{void
}{Delete
}{\void}
70 Calling
\helpref{Delete
}{wxthreaddelete
} is a graceful way to terminate the
71 thread. It asks the thread to terminate and, if the thread code is well
72 written, the thread will terminate after the next call to
73 \helpref{TestDestroy
}{wxthreadtestdestroy
} which should happen quiet soon.
75 However, if the thread doesn't call
\helpref{TestDestroy
}{wxthreadtestdestroy
}
76 often enough (or at all), the function will not return immediately, but wait
77 until the thread terminates. As it may take a long time, the message processing
78 is not stopped during this function execution, so the message handlers may be
79 called from inside it!
81 Delete() may be called for thread in any state: running, paused or even not yet
82 created. Moreover, it must be called if
\helpref{Create
}{wxthreadcreate
} or
83 \helpref{Run
}{wxthreadrun
} failed for a detached thread to free the memory
84 occupied by the thread object (it will be done in the destructor for joinable
87 For detached threads Delete() will also delete the C++ thread object, but it
88 will not do this for joinable ones.
90 This function can only be called from another thread context.
92 \membersection{wxThread::Entry
}\label{wxthreadentry
}
94 \func{virtual ExitCode
}{Entry
}{\void}
96 This is the entry point of the thread. This function is pure virtual and must
97 be implemented by any derived class. The thread execution will start here.
99 The returned value is the thread exit code which is only useful for the
100 joinable threads and is the value returned by
\helpref{Wait
}{wxthreadwait
}.
102 This function is called by wxWindows itself and should never be called
105 \membersection{wxThread::GetId
}\label{wxthreadgetid
}
107 \constfunc{unsigned long
}{GetId
}{\void}
109 Gets the thread identifier: this is a platform dependent number which uniquely identifies the
110 thread throughout the system during its existence (i.e. the thread identifiers may be reused).
112 \membersection{wxThread::GetPriority
}\label{wxthreadgetpriority
}
114 \constfunc{int
}{GetPriority
}{\void}
116 Gets the priority of the thread, between zero and
100.
118 The following priorities are defined:
121 \begin{twocollist
}\itemsep=
0pt
122 \twocolitem{{\bf WXTHREAD
\_MIN\_PRIORITY}}{0}
123 \twocolitem{{\bf WXTHREAD
\_DEFAULT\_PRIORITY}}{50}
124 \twocolitem{{\bf WXTHREAD
\_MAX\_PRIORITY}}{100}
127 \membersection{wxThread::IsAlive
}\label{wxthreadisalive
}
129 \constfunc{bool
}{IsAlive
}{\void}
131 Returns TRUE if the thread is alive (i.e. started and not terminating).
133 \membersection{wxThread::IsDetached
}\label{wxthreadisdetached
}
135 \constfunc{bool
}{IsDetached
}{\void}
137 Returns TRUE if the thread is of detached kind, FALSE if it is a joinable one.
139 \membersection{wxThread::IsMain
}\label{wxthreadismain
}
141 \func{static bool
}{IsMain
}{\void}
143 Returns TRUE if the calling thread is the main application thread.
145 \membersection{wxThread::IsPaused
}\label{wxthreadispaused
}
147 \constfunc{bool
}{IsPaused
}{\void}
149 Returns TRUE if the thread is paused.
151 \membersection{wxThread::IsRunning
}\label{wxthreadisrunning
}
153 \constfunc{bool
}{IsRunning
}{\void}
155 Returns TRUE if the thread is running.
157 \membersection{wxThread::Kill
}\label{wxthreadkill
}
159 \func{wxThreadError
}{Kill
}{\void}
161 Immediately terminates the target thread.
{\bf This function is dangerous and should
162 be used with extreme care (and not used at all whenever possible)!
} The resources
163 allocated to the thread will not be freed and the state of the C runtime library
164 may become inconsistent. Use
\helpref{Delete()
}{wxthreaddelete
} instead.
166 For detached threads Kill() will also delete the associated C++ object.
168 This function can only be called from another thread context.
170 \membersection{wxThread::OnExit
}\label{wxthreadonexit
}
172 \func{void
}{OnExit
}{\void}
174 Called when the thread exits. This function is called in the context of the thread
175 associated with the wxThread object, not in the context of the main thread.
177 This function should never be called directly.
179 \membersection{wxThread::Pause
}\label{wxthreadpause
}
181 \func{wxThreadError
}{Pause
}{\void}
183 Suspends the thread. Under some implementations (Win32), the thread is
184 suspended immediately, under others it will only be suspended when it calls
185 \helpref{TestDestroy
}{wxthreadtestdestroy
} for the next time (hence, if the
186 thread doesn't call it at all, it won't be suspended).
188 This function can only be called from another thread context.
190 \membersection{wxThread::Run
}\label{wxthreadrun
}
192 \func{wxThreadError
}{Run
}{\void}
194 Starts the thread execution. Should be called after
195 \helpref{Create
}{wxthreadcreate
}.
197 This function can only be called from another thread context.
199 \membersection{wxThread::SetPriority
}\label{wxthreadsetpriority
}
201 \func{void
}{SetPriority
}{\param{int
}{ priority
}}
203 Sets the priority of the thread, between zero and
100. This must be set before the thread is created.
205 The following priorities are already defined:
208 \begin{twocollist
}\itemsep=
0pt
209 \twocolitem{{\bf WXTHREAD
\_MIN\_PRIORITY}}{0}
210 \twocolitem{{\bf WXTHREAD
\_DEFAULT\_PRIORITY}}{50}
211 \twocolitem{{\bf WXTHREAD
\_MAX\_PRIORITY}}{100}
214 \membersection{wxThread::Sleep
}\label{wxthreadsleep
}
216 \func{static void
}{Sleep
}{\param{unsigned long
}{milliseconds
}}
218 Pauses the thread execution for the given amount of time.
220 This function should be used instead of
\helpref{wxSleep
}{wxsleep
} by all worker
221 (i.e. all except the main one) threads.
223 \membersection{wxThread::Resume
}\label{wxthreadresume
}
225 \func{wxThreadError
}{Resume
}{\void}
227 Resumes a thread suspended by the call to
\helpref{Pause
}{wxthreadpause
}.
229 This function can only be called from another thread context.
231 \membersection{wxThread::TestDestroy
}\label{wxthreadtestdestroy
}
233 \func{bool
}{TestDestroy
}{\void}
235 This function should be periodically called by the thread to ensure that calls
236 to
\helpref{Pause
}{wxthreadpause
} and
\helpref{Delete
}{wxthreaddelete
} will
237 work. If it returns TRUE, the thread should exit as soon as possible.
239 \membersection{wxThread::This
}\label{wxthreadthis
}
241 \func{static wxThread *
}{This
}{\void}
243 Return the thread object for the calling thread. NULL is returned if the calling thread
244 is the main (GUI) thread, but
\helpref{IsMain
}{wxthreadismain
} should be used to test
245 whether the thread is really the main one because NULL may also be returned for the thread
246 not created with wxThread class. Generally speaking, the return value for such thread
249 \membersection{wxThread::Yield
}\label{wxthreadyield
}
251 \func{void
}{Yield
}{\void}
253 Give the rest of the thread time slice to the system allowing the other threads to run.
254 See also
\helpref{Sleep()
}{wxthreadsleep
}.
256 \membersection{wxThread::Wait
}\label{wxthreadwait
}
258 \constfunc{ExitCode
}{Wait
}{\void}
260 Waits until the thread terminates and returns its exit code or
{\tt
261 (ExitCode)-
1} on error.
263 You can only Wait() for joinable (not detached) threads.
265 This function can only be called from another thread context.