1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
<HTML>
<head><title>wxThread</title></head>
<BODY BGCOLOR=#FFFFFF>
<A NAME="wxthread"></A><CENTER>
<A HREF="wx.htm"><img align=center src="contents.gif" BORDER=0 ALT="Contents"></A> <A HREF="wx22.htm#classref"><img align=center src="up.gif" BORDER=0 ALT="Up"></A> <A HREF="wx245.htm#wxtextfile"><img align=center src="back.gif" BORDER=0 ALT="Previous"></A> <A HREF="wx247.htm#wxtime"><img align=center src="forward.gif" BORDER=0 ALT="Next"></A> </CENTER><HR>
<H2>wxThread</H2>
<P>
A thread is basically a path of execution through a program. Threads are also
sometimes called <I>light-weight processes</I>, but the fundamental difference
between threads and processes is that memory spaces of different processes are
separated while all threads share the same address space. While it makes it
much easier to share common data between several threads, it also makes much
easier to shoot oneself in the foot, so careful use of synchronization objects
such as <A HREF="wx153.htm#wxmutex">mutexes</A> and/or <A HREF="wx57.htm#wxcriticalsection">critical sections</A> is recommended.<P>
<B><FONT COLOR="#FF0000">Derived from</FONT></B><P>
None.<P>
<B><FONT COLOR="#FF0000">Include files</FONT></B><P>
<wx/thread.h><P>
<B><FONT COLOR="#FF0000">See also</FONT></B><P>
<A HREF="wx153.htm#wxmutex">wxMutex</A>, <A HREF="wx54.htm#wxcondition">wxCondition</A>, <A HREF="wx57.htm#wxcriticalsection">wxCriticalSection</A><P>
<B><FONT COLOR="#FF0000">Members</FONT></B><P>
<A HREF="#wxthreadctor">wxThread::wxThread</A><BR>
<A HREF="#topic976">wxThread::~wxThread</A><BR>
<A HREF="#wxthreadcreate">wxThread::Create</A><BR>
<A HREF="#wxthreaddelete">wxThread::Delete</A><BR>
<A HREF="#wxthreadgetid">wxThread::GetID</A><BR>
<A HREF="#wxthreadgetpriority">wxThread::GetPriority</A><BR>
<A HREF="#wxthreadisalive">wxThread::IsAlive</A><BR>
<A HREF="#wxthreadismain">wxThread::IsMain</A><BR>
<A HREF="#wxthreadispaused">wxThread::IsPaused</A><BR>
<A HREF="#wxthreadisrunning">wxThread::IsRunning</A><BR>
<A HREF="#wxthreadkill">wxThread::Kill</A><BR>
<A HREF="#wxthreadonexit">wxThread::OnExit</A><BR>
<A HREF="#wxthreadrun">wxThread::Run</A><BR>
<A HREF="#wxthreadsetpriority">wxThread::SetPriority</A><BR>
<A HREF="#wxthreadsleep">wxThread::Sleep</A><BR>
<A HREF="#wxthreadthis">wxThread::This</A><BR>
<A HREF="#wxthreadyield">wxThread::Yield</A><BR>
<P>
<HR>
<A NAME="wxthreadctor"></A>
<H3>wxThread::wxThread</H3>
<P>
<B></B> <B>wxThread</B>()<P>
Default constructor: it doesn't create nor starts the thread.<P>
<HR>
<A NAME="topic976"></A>
<H3>wxThread::~wxThread</H3>
<P>
<B></B> <B>~wxThread</B>()<P>
wxThread destructor is private, so you can not call it directly - i.e., deleting
wxThread objects is forbidden. Instead, you should use <A HREF="wx246.htm#wxthreaddelete">Delete</A> or
<A HREF="wx246.htm#wxthreadkill">Kill</A> methods. This also means that thread objects should
eb <B>always</B> allocated on the heap (i.e. with <I>new</I>) because the functions
mentioned above will try to reclaim the storage from the heap.<P>
<HR>
<A NAME="wxthreadcreate"></A>
<H3>wxThread::Create</H3>
<P>
<B>wxThreadError</B> <B>Create</B>()<P>
Creates a new thread. The thread object is created in the suspended state, you
should call <A HREF="wx246.htm#wxthreadrun">Run</A> to start running it.<P>
<B><FONT COLOR="#FF0000">Return value</FONT></B><P>
One of:<P>
<TABLE>
<TR><TD VALIGN=TOP>
<B>wxTHREAD_NO_ERROR</B>
</TD>
<TD VALIGN=TOP>
There was no error.
</TD></TR>
<TR><TD VALIGN=TOP>
<B>wxTHREAD_NO_RESOURCE</B>
</TD>
<TD VALIGN=TOP>
There were insufficient resources to create a new thread.
</TD></TR>
<TR><TD VALIGN=TOP>
<B>wxTHREAD_RUNNING</B>
</TD>
<TD VALIGN=TOP>
The thread is already running.
</TD></TR>
</TABLE>
<P>
<HR>
<A NAME="wxthreaddelete"></A>
<H3>wxThread::Delete</H3>
<P>
<B></B> <B>Delete</B>()<P>
This function should be called to terminate this thread. Unlike <A HREF="wx246.htm#wxthreadkill">Kill</A>, it
gives the target thread the time to terminate gracefully. Because of this, however, this function
may not return immediately and if the thread is "hung" won't return at all. Also, message processing
is not stopped during this function execution, so the message handlers may be called from inside
it.<P>
Delete() may be called for thread in any state: running, paused or even not yet created. Moreover,
it must be called if <A HREF="wx246.htm#wxthreadcreate">Create</A> or <A HREF="wx246.htm#wxthreadrun">Run</A> fail to free
the memory occupied by the thread object.<P>
<HR>
<A NAME="wxthreadgetid"></A>
<H3>wxThread::GetID</H3>
<P>
<B>unsigned long</B> <B>GetID</B>() <B>const</B><P>
Gets the thread identifier: this is a platform dependent number which uniquely identifies the
thread throughout the system during its existence (i.e. the thread identifiers may be reused).<P>
<HR>
<A NAME="wxthreadgetpriority"></A>
<H3>wxThread::GetPriority</H3>
<P>
<B>int</B> <B>GetPriority</B>() <B>const</B><P>
Gets the priority of the thread, between zero and 100.<P>
The following priorities are already defined:<P>
<TABLE>
<TR><TD VALIGN=TOP>
<B>WXTHREAD_MIN_PRIORITY</B>
</TD>
<TD VALIGN=TOP>
0
</TD></TR>
<TR><TD VALIGN=TOP>
<B>WXTHREAD_DEFAULT_PRIORITY</B>
</TD>
<TD VALIGN=TOP>
50
</TD></TR>
<TR><TD VALIGN=TOP>
<B>WXTHREAD_MAX_PRIORITY</B>
</TD>
<TD VALIGN=TOP>
100
</TD></TR>
</TABLE>
<P>
<HR>
<A NAME="wxthreadisalive"></A>
<H3>wxThread::IsAlive</H3>
<P>
<B>bool</B> <B>IsAlive</B>() <B>const</B><P>
Returns TRUE if the thread is alive (i.e. started and not terminating).<P>
<HR>
<A NAME="wxthreadismain"></A>
<H3>wxThread::IsMain</H3>
<P>
<B>bool</B> <B>IsMain</B>() <B>const</B><P>
Returns TRUE if the calling thread is the main application thread.<P>
<HR>
<A NAME="wxthreadispaused"></A>
<H3>wxThread::IsPaused</H3>
<P>
<B>bool</B> <B>IsPaused</B>() <B>const</B><P>
Returns TRUE if the thread is paused.<P>
<HR>
<A NAME="wxthreadisrunning"></A>
<H3>wxThread::IsRunning</H3>
<P>
<B>bool</B> <B>IsRunning</B>() <B>const</B><P>
Returns TRUE if the thread is running.<P>
<HR>
<A NAME="wxthreadkill"></A>
<H3>wxThread::Kill</H3>
<P>
<B>wxThreadError</B> <B>Kill</B>()<P>
Immediately terminates the target thread. <B>This function is dangerous and should
be used with extreme care (and not used at all whenever possible)!</B> The resources
allocated to the thread will not be freed and the state of the C runtime library
may become inconsistent. Use <A HREF="wx246.htm#wxthreaddelete">Delete()</A> instead.<P>
<HR>
<A NAME="wxthreadonexit"></A>
<H3>wxThread::OnExit</H3>
<P>
<B>void</B> <B>OnExit</B>()<P>
Called when the thread exits. This function is called in the context of the thread
associated with the wxThread object, not in the context of the main thread.<P>
<HR>
<A NAME="wxthreadrun"></A>
<H3>wxThread::Run</H3>
<P>
<B>wxThreadError</B> <B>Run</B>()<P>
Runs the thread.<P>
<HR>
<A NAME="wxthreadsetpriority"></A>
<H3>wxThread::SetPriority</H3>
<P>
<B>void</B> <B>SetPriority</B>(<B>int</B><I> priority</I>)<P>
Sets the priority of the thread, between zero and 100. This must be set before the thread is created.<P>
The following priorities are already defined:<P>
<TABLE>
<TR><TD VALIGN=TOP>
<B>WXTHREAD_MIN_PRIORITY</B>
</TD>
<TD VALIGN=TOP>
0
</TD></TR>
<TR><TD VALIGN=TOP>
<B>WXTHREAD_DEFAULT_PRIORITY</B>
</TD>
<TD VALIGN=TOP>
50
</TD></TR>
<TR><TD VALIGN=TOP>
<B>WXTHREAD_MAX_PRIORITY</B>
</TD>
<TD VALIGN=TOP>
100
</TD></TR>
</TABLE>
<P>
<HR>
<A NAME="wxthreadsleep"></A>
<H3>wxThread::Sleep</H3>
<P>
<B></B> <B>Sleep</B>(<B>unsigned long </B><I>milliseconds</I>)<P>
Pauses the thread execution for the given amount of time.<P>
This function should be used instead of <A HREF="wx271.htm#wxsleep">wxSleep</A> by all worker
(i.e. all except the main one) threads.<P>
<HR>
<A NAME="wxthreadthis"></A>
<H3>wxThread::This</H3>
<P>
<B>wxThread *</B> <B>This</B>()<P>
Return the thread object for the calling thread. NULL is returned if the calling thread
is the main (GUI) thread, but <A HREF="wx246.htm#wxthreadismain">IsMain</A> should be used to test
whether the thread is really the main one because NULL may also be returned for the thread
not created with wxThread class. Generally speaking, the return value for such thread
is undefined.<P>
<HR>
<A NAME="wxthreadyield"></A>
<H3>wxThread::Yield</H3>
<P>
<B></B> <B>Yield</B>()<P>
Give the rest of the thread time slice to the system allowing the other threads to run.
See also <A HREF="wx246.htm#wxthreadsleep">Sleep()</A>.<P>
</BODY></HTML>
|