[190] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
| 2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/kernel/qthread.cpp:52 -->
|
---|
| 3 | <html>
|
---|
| 4 | <head>
|
---|
| 5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
| 6 | <title>QThread Class</title>
|
---|
| 7 | <style type="text/css"><!--
|
---|
| 8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
| 9 | a:link { color: #004faf; text-decoration: none }
|
---|
| 10 | a:visited { color: #672967; text-decoration: none }
|
---|
| 11 | body { background: #ffffff; color: black; }
|
---|
| 12 | --></style>
|
---|
| 13 | </head>
|
---|
| 14 | <body>
|
---|
| 15 |
|
---|
| 16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
| 17 | <tr bgcolor="#E5E5E5">
|
---|
| 18 | <td valign=center>
|
---|
| 19 | <a href="index.html">
|
---|
| 20 | <font color="#004faf">Home</font></a>
|
---|
| 21 | | <a href="classes.html">
|
---|
| 22 | <font color="#004faf">All Classes</font></a>
|
---|
| 23 | | <a href="mainclasses.html">
|
---|
| 24 | <font color="#004faf">Main Classes</font></a>
|
---|
| 25 | | <a href="annotated.html">
|
---|
| 26 | <font color="#004faf">Annotated</font></a>
|
---|
| 27 | | <a href="groups.html">
|
---|
| 28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
| 29 | | <a href="functions.html">
|
---|
| 30 | <font color="#004faf">Functions</font></a>
|
---|
| 31 | </td>
|
---|
| 32 | <td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QThread Class Reference</h1>
|
---|
| 33 |
|
---|
| 34 | <p>The QThread class provides platform-independent threads.
|
---|
| 35 | <a href="#details">More...</a>
|
---|
| 36 | <p>All the functions in this class are <a href="threads.html#threadsafe">thread-safe</a> when Qt is built with thread support.</p>
|
---|
| 37 | <p><tt>#include <<a href="qthread-h.html">qthread.h</a>></tt>
|
---|
| 38 | <p>Inherits <a href="qt.html">Qt</a>.
|
---|
| 39 | <p><a href="qthread-members.html">List of all member functions.</a>
|
---|
| 40 | <h2>Public Members</h2>
|
---|
| 41 | <ul>
|
---|
| 42 | <li class=fn><a href="#QThread"><b>QThread</b></a> ( unsigned int stackSize = 0 )</li>
|
---|
| 43 | <li class=fn>virtual <a href="#~QThread"><b>~QThread</b></a> ()</li>
|
---|
| 44 | <li class=fn>bool <a href="#wait"><b>wait</b></a> ( unsigned long time = ULONG_MAX )</li>
|
---|
| 45 | <li class=fn>enum <a href="#Priority-enum"><b>Priority</b></a> { IdlePriority, LowestPriority, LowPriority, NormalPriority, HighPriority, HighestPriority, TimeCriticalPriority, InheritPriority }</li>
|
---|
| 46 | <li class=fn>void <a href="#start"><b>start</b></a> ( Priority priority = InheritPriority )</li>
|
---|
| 47 | <li class=fn>void <a href="#terminate"><b>terminate</b></a> ()</li>
|
---|
| 48 | <li class=fn>bool <a href="#finished"><b>finished</b></a> () const</li>
|
---|
| 49 | <li class=fn>bool <a href="#running"><b>running</b></a> () const</li>
|
---|
| 50 | </ul>
|
---|
| 51 | <h2>Static Public Members</h2>
|
---|
| 52 | <ul>
|
---|
| 53 | <li class=fn>Qt::HANDLE <a href="#currentThread"><b>currentThread</b></a> ()</li>
|
---|
| 54 | <li class=fn>void postEvent ( QObject * receiver, QEvent * event ) <em>(obsolete)</em></li>
|
---|
| 55 | <li class=fn>void <a href="#exit"><b>exit</b></a> ()</li>
|
---|
| 56 | </ul>
|
---|
| 57 | <h2>Protected Members</h2>
|
---|
| 58 | <ul>
|
---|
| 59 | <li class=fn>virtual void <a href="#run"><b>run</b></a> () = 0</li>
|
---|
| 60 | </ul>
|
---|
| 61 | <h2>Static Protected Members</h2>
|
---|
| 62 | <ul>
|
---|
| 63 | <li class=fn>void <a href="#sleep"><b>sleep</b></a> ( unsigned long secs )</li>
|
---|
| 64 | <li class=fn>void <a href="#msleep"><b>msleep</b></a> ( unsigned long msecs )</li>
|
---|
| 65 | <li class=fn>void <a href="#usleep"><b>usleep</b></a> ( unsigned long usecs )</li>
|
---|
| 66 | </ul>
|
---|
| 67 | <hr><a name="details"></a><h2>Detailed Description</h2>
|
---|
| 68 |
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | The QThread class provides platform-independent threads.
|
---|
| 72 | <p>
|
---|
| 73 |
|
---|
| 74 | <p> A QThread represents a separate thread of control within the
|
---|
| 75 | program; it shares data with all the other threads within the
|
---|
| 76 | process but executes independently in the way that a separate
|
---|
| 77 | program does on a multitasking operating system. Instead of
|
---|
| 78 | starting in main(), QThreads begin executing in <a href="#run">run</a>(). You inherit
|
---|
| 79 | run() to include your code. For example:
|
---|
| 80 | <p> <pre>
|
---|
| 81 | class MyThread : public QThread {
|
---|
| 82 |
|
---|
| 83 | public:
|
---|
| 84 |
|
---|
| 85 | virtual void run();
|
---|
| 86 |
|
---|
| 87 | };
|
---|
| 88 |
|
---|
| 89 | void MyThread::<a href="#run">run</a>()
|
---|
| 90 | {
|
---|
| 91 | for( int count = 0; count < 20; count++ ) {
|
---|
| 92 | <a href="#sleep">sleep</a>( 1 );
|
---|
| 93 | <a href="qapplication.html#qDebug">qDebug</a>( "Ping!" );
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | int main()
|
---|
| 98 | {
|
---|
| 99 | MyThread a;
|
---|
| 100 | MyThread b;
|
---|
| 101 | a.<a href="#start">start</a>();
|
---|
| 102 | b.<a href="#start">start</a>();
|
---|
| 103 | a.<a href="#wait">wait</a>();
|
---|
| 104 | b.<a href="#wait">wait</a>();
|
---|
| 105 | }
|
---|
| 106 | </pre>
|
---|
| 107 |
|
---|
| 108 | <p> This will start two threads, each of which writes Ping! 20 times
|
---|
| 109 | to the screen and exits. The <a href="#wait">wait</a>() calls at the end of main() are
|
---|
| 110 | necessary because exiting main() ends the program, unceremoniously
|
---|
| 111 | killing all other threads. Each MyThread stops executing when it
|
---|
| 112 | reaches the end of MyThread::run(), just as an application does
|
---|
| 113 | when it leaves main().
|
---|
| 114 | <p> <p>See also <a href="threads.html">Thread Support in Qt</a>, <a href="environment.html">Environment Classes</a>, and <a href="thread.html">Threading</a>.
|
---|
| 115 |
|
---|
| 116 | <hr><h2>Member Type Documentation</h2>
|
---|
| 117 | <h3 class=fn><a name="Priority-enum"></a>QThread::Priority</h3>
|
---|
| 118 |
|
---|
| 119 | <p> This enum type indicates how the operating system should schedule
|
---|
| 120 | newly created threads.
|
---|
| 121 | <ul>
|
---|
| 122 | <li><tt>QThread::IdlePriority</tt> - scheduled only when no other threads are
|
---|
| 123 | running.
|
---|
| 124 | <li><tt>QThread::LowestPriority</tt> - scheduled less often than LowPriority.
|
---|
| 125 | <li><tt>QThread::LowPriority</tt> - scheduled less often than NormalPriority.
|
---|
| 126 | <li><tt>QThread::NormalPriority</tt> - the default priority of the operating
|
---|
| 127 | system.
|
---|
| 128 | <li><tt>QThread::HighPriority</tt> - scheduled more often than NormalPriority.
|
---|
| 129 | <li><tt>QThread::HighestPriority</tt> - scheduled more often then HighPriority.
|
---|
| 130 | <li><tt>QThread::TimeCriticalPriority</tt> - scheduled as often as possible.
|
---|
| 131 | <li><tt>QThread::InheritPriority</tt> - use the same priority as the creating
|
---|
| 132 | thread. This is the default.
|
---|
| 133 | </ul>
|
---|
| 134 | <hr><h2>Member Function Documentation</h2>
|
---|
| 135 | <h3 class=fn><a name="QThread"></a>QThread::QThread ( unsigned int stackSize = 0 )
|
---|
| 136 | </h3>
|
---|
| 137 | Constructs a new thread. The thread does not begin executing until
|
---|
| 138 | <a href="#start">start</a>() is called.
|
---|
| 139 | <p> If <em>stackSize</em> is greater than zero, the maximum stack size is
|
---|
| 140 | set to <em>stackSize</em> bytes, otherwise the maximum stack size is
|
---|
| 141 | automatically determined by the operating system.
|
---|
| 142 | <p> <b>Warning:</b> Most operating systems place minimum and maximum limits
|
---|
| 143 | on thread stack sizes. The thread will fail to start if the stack
|
---|
| 144 | size is outside these limits.
|
---|
| 145 |
|
---|
| 146 | <h3 class=fn><a name="~QThread"></a>QThread::~QThread ()<tt> [virtual]</tt>
|
---|
| 147 | </h3>
|
---|
| 148 | QThread destructor.
|
---|
| 149 | <p> Note that deleting a QThread object will not stop the execution of
|
---|
| 150 | the thread it represents. Deleting a running QThread (i.e.
|
---|
| 151 | <a href="#finished">finished</a>() returns FALSE) will probably result in a program crash.
|
---|
| 152 | You can <a href="#wait">wait</a>() on a thread to make sure that it has finished.
|
---|
| 153 |
|
---|
| 154 | <h3 class=fn>Qt::HANDLE <a name="currentThread"></a>QThread::currentThread ()<tt> [static]</tt>
|
---|
| 155 | </h3>
|
---|
| 156 | This returns the thread handle of the currently executing thread.
|
---|
| 157 | <p> <b>Warning:</b> The handle returned by this function is used for internal
|
---|
| 158 | purposes and should <em>not</em> be used in any application code. On
|
---|
| 159 | Windows, the returned value is a pseudo handle for the current
|
---|
| 160 | thread, and it cannot be used for numerical comparison.
|
---|
| 161 |
|
---|
| 162 | <h3 class=fn>void <a name="exit"></a>QThread::exit ()<tt> [static]</tt>
|
---|
| 163 | </h3>
|
---|
| 164 | Ends the execution of the calling thread and wakes up any threads
|
---|
| 165 | waiting for its termination.
|
---|
| 166 |
|
---|
| 167 | <h3 class=fn>bool <a name="finished"></a>QThread::finished () const
|
---|
| 168 | </h3>
|
---|
| 169 | Returns TRUE if the thread is finished; otherwise returns FALSE.
|
---|
| 170 |
|
---|
| 171 | <h3 class=fn>void <a name="msleep"></a>QThread::msleep ( unsigned long msecs )<tt> [static protected]</tt>
|
---|
| 172 | </h3>
|
---|
| 173 | System independent sleep. This causes the current thread to sleep
|
---|
| 174 | for <em>msecs</em> milliseconds
|
---|
| 175 |
|
---|
| 176 | <h3 class=fn>void <a name="postEvent"></a>QThread::postEvent ( <a href="qobject.html">QObject</a> * receiver, <a href="qevent.html">QEvent</a> * event )<tt> [static]</tt>
|
---|
| 177 | </h3> <b>This function is obsolete.</b> It is provided to keep old source working. We strongly advise against using it in new code.
|
---|
| 178 | <p> Use <a href="qapplication.html#postEvent">QApplication::postEvent</a>() instead.
|
---|
| 179 |
|
---|
| 180 | <h3 class=fn>void <a name="run"></a>QThread::run ()<tt> [pure virtual protected]</tt>
|
---|
| 181 | </h3>
|
---|
| 182 |
|
---|
| 183 | <p> This method is pure virtual, and must be implemented in derived
|
---|
| 184 | classes in order to do useful work. Returning from this method
|
---|
| 185 | will end the execution of the thread.
|
---|
| 186 | <p> <p>See also <a href="#wait">wait</a>().
|
---|
| 187 |
|
---|
| 188 | <h3 class=fn>bool <a name="running"></a>QThread::running () const
|
---|
| 189 | </h3>
|
---|
| 190 | Returns TRUE if the thread is running; otherwise returns FALSE.
|
---|
| 191 |
|
---|
| 192 | <h3 class=fn>void <a name="sleep"></a>QThread::sleep ( unsigned long secs )<tt> [static protected]</tt>
|
---|
| 193 | </h3>
|
---|
| 194 | System independent sleep. This causes the current thread to sleep
|
---|
| 195 | for <em>secs</em> seconds.
|
---|
| 196 |
|
---|
| 197 | <h3 class=fn>void <a name="start"></a>QThread::start ( <a href="qthread.html#Priority-enum">Priority</a> priority = InheritPriority )
|
---|
| 198 | </h3>
|
---|
| 199 | Begins execution of the thread by calling <a href="#run">run</a>(), which should be
|
---|
| 200 | reimplemented in a QThread subclass to contain your code. The
|
---|
| 201 | operating system will schedule the thread according to the <em>priority</em> argument.
|
---|
| 202 | <p> If you try to start a thread that is already running, this
|
---|
| 203 | function will wait until the the thread has finished and then
|
---|
| 204 | restart the thread.
|
---|
| 205 | <p> <p>See also <a href="#Priority-enum">Priority</a>.
|
---|
| 206 |
|
---|
| 207 | <h3 class=fn>void <a name="terminate"></a>QThread::terminate ()
|
---|
| 208 | </h3>
|
---|
| 209 | This function terminates the execution of the thread. The thread
|
---|
| 210 | may or may not be terminated immediately, depending on the
|
---|
| 211 | operating system's scheduling policies. Use <a href="#wait">QThread::wait</a>()
|
---|
| 212 | after <a href="#terminate">terminate</a>() for synchronous termination.
|
---|
| 213 | <p> When the thread is terminated, all threads waiting for the
|
---|
| 214 | the thread to finish will be woken up.
|
---|
| 215 | <p> <b>Warning:</b> This function is dangerous, and its use is discouraged.
|
---|
| 216 | The thread can be terminated at any point in its code path. Threads
|
---|
| 217 | can be terminated while modifying data. There is no chance for
|
---|
| 218 | the thread to cleanup after itself, unlock any held mutexes, etc.
|
---|
| 219 | In short, use this function only if <em>absolutely</em> necessary.
|
---|
| 220 |
|
---|
| 221 | <h3 class=fn>void <a name="usleep"></a>QThread::usleep ( unsigned long usecs )<tt> [static protected]</tt>
|
---|
| 222 | </h3>
|
---|
| 223 | System independent sleep. This causes the current thread to sleep
|
---|
| 224 | for <em>usecs</em> microseconds
|
---|
| 225 |
|
---|
| 226 | <h3 class=fn>bool <a name="wait"></a>QThread::wait ( unsigned long time = ULONG_MAX )
|
---|
| 227 | </h3>
|
---|
| 228 | A thread calling this function will block until either of these
|
---|
| 229 | conditions is met:
|
---|
| 230 | <p> <ul>
|
---|
| 231 | <li> The thread associated with this QThread object has finished
|
---|
| 232 | execution (i.e. when it returns from <a href="#run">run</a>()). This function
|
---|
| 233 | will return TRUE if the thread has finished. It also returns
|
---|
| 234 | TRUE if the thread has not been started yet.
|
---|
| 235 | <li> <em>time</em> milliseconds has elapsed. If <em>time</em> is ULONG_MAX (the
|
---|
| 236 | default), then the wait will never timeout (the thread must
|
---|
| 237 | return from <a href="#run">run</a>()). This function will return FALSE if the
|
---|
| 238 | wait timed out.
|
---|
| 239 | </ul>
|
---|
| 240 | <p> This provides similar functionality to the POSIX <tt>pthread_join()</tt> function.
|
---|
| 241 |
|
---|
| 242 | <!-- eof -->
|
---|
| 243 | <hr><p>
|
---|
| 244 | This file is part of the <a href="index.html">Qt toolkit</a>.
|
---|
| 245 | Copyright © 1995-2007
|
---|
| 246 | <a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
|
---|
| 247 | <table width=100% cellspacing=0 border=0><tr>
|
---|
| 248 | <td>Copyright © 2007
|
---|
| 249 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
| 250 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
| 251 | </table></div></address></body>
|
---|
| 252 | </html>
|
---|