1QThread(3qt) QThread(3qt)
2
3
4
6 QThread - Platform-independent threads
7
9 All the functions in this class are thread-safe when Qt is built with
10 thread support.</p>
11
12 #include <qthread.h>
13
14 Inherits Qt.
15
16 Public Members
17 QThread ( unsigned int stackSize = 0 )
18 virtual ~QThread ()
19 bool wait ( unsigned long time = ULONG_MAX )
20 enum Priority { IdlePriority, LowestPriority, LowPriority,
21 NormalPriority, HighPriority, HighestPriority,
22 TimeCriticalPriority, InheritPriority }
23 void start ( Priority priority = InheritPriority )
24 void terminate ()
25 bool finished () const
26 bool running () const
27
28 Static Public Members
29 Qt::HANDLE currentThread ()
30 void postEvent ( QObject * receiver, QEvent * event ) (obsolete)
31 void exit ()
32
33 Protected Members
34 virtual void run () = 0
35
36 Static Protected Members
37 void sleep ( unsigned long secs )
38 void msleep ( unsigned long msecs )
39 void usleep ( unsigned long usecs )
40
42 The QThread class provides platform-independent threads.
43
44 A QThread represents a separate thread of control within the program;
45 it shares data with all the other threads within the process but
46 executes independently in the way that a separate program does on a
47 multitasking operating system. Instead of starting in main(), QThreads
48 begin executing in run(). You inherit run() to include your code. For
49 example:
50
51 class MyThread : public QThread {
52 public:
53 virtual void run();
54 };
55 void MyThread::run()
56 {
57 for( int count = 0; count < 20; count++ ) {
58 sleep( 1 );
59 qDebug( "Ping!" );
60 }
61 }
62 int main()
63 {
64 MyThread a;
65 MyThread b;
66 a.start();
67 b.start();
68 a.wait();
69 b.wait();
70 }
71
72 This will start two threads, each of which writes Ping! 20 times to the
73 screen and exits. The wait() calls at the end of main() are necessary
74 because exiting main() ends the program, unceremoniously killing all
75 other threads. Each MyThread stops executing when it reaches the end of
76 MyThread::run(), just as an application does when it leaves main().
77
78 See also Thread Support in Qt, Environment Classes, and Threading.
79
80 Member Type Documentation
82 This enum type indicates how the operating system should schedule newly
83 created threads.
84
85 QThread::IdlePriority - scheduled only when no other threads are
86 running.
87
88 QThread::LowestPriority - scheduled less often than LowPriority.
89
90 QThread::LowPriority - scheduled less often than NormalPriority.
91
92 QThread::NormalPriority - the default priority of the operating system.
93
94 QThread::HighPriority - scheduled more often than NormalPriority.
95
96 QThread::HighestPriority - scheduled more often then HighPriority.
97
98 QThread::TimeCriticalPriority - scheduled as often as possible.
99
100 QThread::InheritPriority - use the same priority as the creating
101 thread. This is the default.
102
105 Constructs a new thread. The thread does not begin executing until
106 start() is called.
107
108 If stackSize is greater than zero, the maximum stack size is set to
109 stackSize bytes, otherwise the maximum stack size is automatically
110 determined by the operating system.
111
112 Warning: Most operating systems place minimum and maximum limits on
113 thread stack sizes. The thread will fail to start if the stack size is
114 outside these limits.
115
117 QThread destructor.
118
119 Note that deleting a QThread object will not stop the execution of the
120 thread it represents. Deleting a running QThread (i.e. finished()
121 returns FALSE) will probably result in a program crash. You can wait()
122 on a thread to make sure that it has finished.
123
125 This returns the thread handle of the currently executing thread.
126
127 Warning: The handle returned by this function is used for internal
128 purposes and should not be used in any application code. On Windows,
129 the returned value is a pseudo handle for the current thread, and it
130 cannot be used for numerical comparison.
131
133 Ends the execution of the calling thread and wakes up any threads
134 waiting for its termination.
135
137 Returns TRUE if the thread is finished; otherwise returns FALSE.
138
140 System independent sleep. This causes the current thread to sleep for
141 msecs milliseconds
142
144 This function is obsolete. It is provided to keep old source working.
145 We strongly advise against using it in new code.
146
147 Use QApplication::postEvent() instead.
148
150 This method is pure virtual, and must be implemented in derived classes
151 in order to do useful work. Returning from this method will end the
152 execution of the thread.
153
154 See also wait().
155
157 Returns TRUE if the thread is running; otherwise returns FALSE.
158
160 System independent sleep. This causes the current thread to sleep for
161 secs seconds.
162
164 Begins execution of the thread by calling run(), which should be
165 reimplemented in a QThread subclass to contain your code. The operating
166 system will schedule the thread according to the priority argument.
167
168 If you try to start a thread that is already running, this function
169 will wait until the the thread has finished and then restart the
170 thread.
171
172 See also Priority.
173
175 This function terminates the execution of the thread. The thread may or
176 may not be terminated immediately, depending on the operating system's
177 scheduling policies. Use QThread::wait() after terminate() for
178 synchronous termination.
179
180 When the thread is terminated, all threads waiting for the the thread
181 to finish will be woken up.
182
183 Warning: This function is dangerous, and its use is discouraged. The
184 thread can be terminated at any point in its code path. Threads can be
185 terminated while modifying data. There is no chance for the thread to
186 cleanup after itself, unlock any held mutexes, etc. In short, use this
187 function only if absolutely necessary.
188
190 System independent sleep. This causes the current thread to sleep for
191 usecs microseconds
192
194 A thread calling this function will block until either of these
195 conditions is met:
196
197 The thread associated with this QThread object has finished execution
198 (i.e. when it returns from run()). This function will return TRUE if
199 the thread has finished. It also returns TRUE if the thread has not
200 been started yet.
201
202 time milliseconds has elapsed. If time is ULONG_MAX (the default), then
203 the wait will never timeout (the thread must return from run()). This
204 function will return FALSE if the wait timed out.
205
206 This provides similar functionality to the POSIX pthread_join()
207 function.
208
209
211 http://doc.trolltech.com/qthread.html
212 http://www.trolltech.com/faq/tech.html
213
215 Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
216 license file included in the distribution for a complete license
217 statement.
218
220 Generated automatically from the source code.
221
223 If you find a bug in Qt, please report it as described in
224 http://doc.trolltech.com/bughowto.html. Good bug reports help us to
225 help you. Thank you.
226
227 The definitive Qt documentation is provided in HTML format; it is
228 located at $QTDIR/doc/html and can be read using Qt Assistant or with a
229 web browser. This man page is provided as a convenience for those users
230 who prefer man pages, although this format is not officially supported
231 by Trolltech.
232
233 If you find errors in this manual page, please report them to qt-
234 bugs@trolltech.com. Please include the name of the manual page
235 (qthread.3qt) and the Qt version (3.3.8).
236
237
238
239Trolltech AS 2 February 2007 QThread(3qt)