source: vendor/trolltech/current/src/kernel/qthread.cpp

Last change on this file was 2, checked in by dmik, 20 years ago

Imported xplatform parts of the official release 3.3.1 from Trolltech

  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1/****************************************************************************
2** $Id: qthread.cpp 2 2005-11-16 15:49:26Z dmik $
3**
4** Cross-platform QThread implementation.
5**
6** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
7**
8** This file is part of the kernel module of the Qt GUI Toolkit.
9**
10** This file may be distributed under the terms of the Q Public License
11** as defined by Trolltech AS of Norway and appearing in the file
12** LICENSE.QPL included in the packaging of this file.
13**
14** This file may be distributed and/or modified under the terms of the
15** GNU General Public License version 2 as published by the Free Software
16** Foundation and appearing in the file LICENSE.GPL included in the
17** packaging of this file.
18**
19** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
20** licenses for Unix/X11 or for Qt/Embedded may use this file in accordance
21** with the Qt Commercial License Agreement provided with the Software.
22**
23** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
25**
26** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27** information about Qt Commercial License Agreements.
28** See http://www.trolltech.com/qpl/ for QPL licensing information.
29** See http://www.trolltech.com/gpl/ for GPL licensing information.
30**
31** Contact info@trolltech.com if any conditions of this licensing are
32** not clear to you.
33**
34**********************************************************************/
35
36#ifdef QT_THREAD_SUPPORT
37
38#include "qplatformdefs.h"
39
40#include "qthread.h"
41#include <private/qthreadinstance_p.h>
42
43#ifndef QT_H
44# include "qapplication.h"
45#endif // QT_H
46
47#if QT_VERSION >= 0x040000
48# error "Remove QThread::QThread() and QThread::start()."
49#endif
50
51
52/*!
53 \class QThread qthread.h
54 \threadsafe
55 \brief The QThread class provides platform-independent threads.
56
57 \ingroup thread
58 \ingroup environment
59
60 A QThread represents a separate thread of control within the
61 program; it shares data with all the other threads within the
62 process but executes independently in the way that a separate
63 program does on a multitasking operating system. Instead of
64 starting in main(), QThreads begin executing in run(). You inherit
65 run() to include your code. For example:
66
67 \code
68 class MyThread : public QThread {
69
70 public:
71
72 virtual void run();
73
74 };
75
76 void MyThread::run()
77 {
78 for( int count = 0; count < 20; count++ ) {
79 sleep( 1 );
80 qDebug( "Ping!" );
81 }
82 }
83
84 int main()
85 {
86 MyThread a;
87 MyThread b;
88 a.start();
89 b.start();
90 a.wait();
91 b.wait();
92 }
93 \endcode
94
95 This will start two threads, each of which writes Ping! 20 times
96 to the screen and exits. The wait() calls at the end of main() are
97 necessary because exiting main() ends the program, unceremoniously
98 killing all other threads. Each MyThread stops executing when it
99 reaches the end of MyThread::run(), just as an application does
100 when it leaves main().
101
102 \sa \link threads.html Thread Support in Qt\endlink.
103*/
104
105/*!
106 \enum QThread::Priority
107
108 This enum type indicates how the operating system should schedule
109 newly created threads.
110
111 \value IdlePriority scheduled only when no other threads are
112 running.
113
114 \value LowestPriority scheduled less often than LowPriority.
115 \value LowPriority scheduled less often than NormalPriority.
116
117 \value NormalPriority the default priority of the operating
118 system.
119
120 \value HighPriority scheduled more often than NormalPriority.
121 \value HighestPriority scheduled more often then HighPriority.
122
123 \value TimeCriticalPriority scheduled as often as possible.
124
125 \value InheritPriority use the same priority as the creating
126 thread. This is the default.
127*/
128
129QThread::QThread()
130{
131 d = new QThreadInstance;
132 d->init(0);
133}
134
135/*!
136 Constructs a new thread. The thread does not begin executing until
137 start() is called.
138
139 If \a stackSize is greater than zero, the maximum stack size is
140 set to \a stackSize bytes, otherwise the maximum stack size is
141 automatically determined by the operating system.
142
143 \warning Most operating systems place minimum and maximum limits
144 on thread stack sizes. The thread will fail to start if the stack
145 size is outside these limits.
146*/
147QThread::QThread( unsigned int stackSize )
148{
149 d = new QThreadInstance;
150 d->init(stackSize);
151}
152
153/*!
154 QThread destructor.
155
156 Note that deleting a QThread object will not stop the execution of
157 the thread it represents. Deleting a running QThread (i.e.
158 finished() returns FALSE) will probably result in a program crash.
159 You can wait() on a thread to make sure that it has finished.
160*/
161QThread::~QThread()
162{
163 QMutexLocker locker( d->mutex() );
164 if ( d->running && !d->finished ) {
165#ifdef QT_CHECK_STATE
166 qWarning("QThread object destroyed while thread is still running.");
167#endif
168
169 d->orphan = TRUE;
170 return;
171 }
172
173 d->deinit();
174 delete d;
175}
176
177/*!
178 This function terminates the execution of the thread. The thread
179 may or may not be terminated immediately, depending on the
180 operating systems scheduling policies. Use QThread::wait()
181 after terminate() for synchronous termination.
182
183 When the thread is terminated, all threads waiting for the
184 the thread to finish will be woken up.
185
186 \warning This function is dangerous, and its use is discouraged.
187 The thread can be terminate at any point in its code path. Threads
188 can be terminated while modifying data. There is no chance for
189 the thread to cleanup after itself, unlock any held mutexes, etc.
190 In short, use this function only if \e absolutely necessary.
191*/
192void QThread::terminate()
193{
194 QMutexLocker locker( d->mutex() );
195 if ( d->finished || !d->running )
196 return;
197 d->terminate();
198}
199
200/*!
201 Returns TRUE if the thread is finished; otherwise returns FALSE.
202*/
203bool QThread::finished() const
204{
205 QMutexLocker locker( d->mutex() );
206 return d->finished;
207}
208
209/*!
210 Returns TRUE if the thread is running; otherwise returns FALSE.
211*/
212bool QThread::running() const
213{
214 QMutexLocker locker( d->mutex() );
215 return d->running;
216}
217
218/*!
219 \fn void QThread::run()
220
221 This method is pure virtual, and must be implemented in derived
222 classes in order to do useful work. Returning from this method
223 will end the execution of the thread.
224
225 \sa wait()
226*/
227
228#ifndef QT_NO_COMPAT
229/*! \obsolete
230 Use QApplication::postEvent() instead.
231*/
232void QThread::postEvent( QObject * receiver, QEvent * event )
233{
234 QApplication::postEvent( receiver, event );
235}
236#endif
237
238#endif // QT_THREAD_SUPPORT
Note: See TracBrowser for help on using the repository browser.