Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Attributes | Friends | List of all members
QThread Class Reference

#include <qthread.h>

Inheritance diagram for QThread:
DotWorkerThread

Public Member Functions

 QThread ()
 
virtual ~QThread ()
 
bool isFinished () const
 
bool isRunning () const
 
void start ()
 
void terminate ()
 
void wait ()
 
void setStackSize (unsigned int stackSize)
 
unsigned int stackSize () const
 

Static Public Member Functions

static int idealThreadCount ()
 

Protected Member Functions

virtual void started ()
 
virtual void finished ()
 
virtual void terminated ()
 
virtual void run ()
 

Private Attributes

QThreadPrivated
 

Friends

class QThreadPrivate
 

Detailed Description

Definition at line 47 of file qthread.h.

Constructor & Destructor Documentation

QThread::QThread ( )
explicit

Definition at line 45 of file qthread.cpp.

46  : d(new QThreadPrivate)
47 {
48 }
QThreadPrivate * d
Definition: qthread.h:73
QThread::~QThread ( )
virtual

Definition at line 50 of file qthread.cpp.

51 {
52  QMutexLocker locker(&d->mutex);
53  if (d->running && !d->finished)
54  qWarning("QThread: Destroyed while thread is still running");
55  delete d;
56 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63

Member Function Documentation

virtual void QThread::finished ( )
inlineprotectedvirtual

Definition at line 66 of file qthread.h.

66 {}
int QThread::idealThreadCount ( )
static

Definition at line 193 of file qthread_unix.cpp.

194 {
195  int cores = -1;
196 #if defined(_OS_MAC_)
197  // Mac OS X
198  cores = (int)MPProcessorsScheduled();
199 #elif defined(_OS_HPUX_)
200  // HP-UX
201  struct pst_dynamic psd;
202  if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) == -1)
203  {
204  perror("pstat_getdynamic");
205  cores = -1;
206  }
207  else
208  {
209  cores = (int)psd.psd_proc_cnt;
210  }
211 #elif defined(_OS_BSDI_)
212  // FreeBSD, OpenBSD, NetBSD, BSD/OS
213  size_t len = sizeof(cores);
214  int mib[2];
215  mib[0] = CTL_HW;
216  mib[1] = HW_NCPU;
217 
218  if (sysctl(mib, 2, &cores, &len, NULL, 0) != 0)
219  {
220  perror("sysctl");
221  cores = -1;
222  }
223 #elif defined(_OS_IRIX_)
224  // IRIX
225  cores = (int)sysconf(_SC_NPROC_ONLN);
226 #else
227  // the rest: Linux, Solaris, AIX, Tru64
228  cores = (int)sysconf(_SC_NPROCESSORS_ONLN);
229 #endif
230  return cores;
231 }
int mib
bool QThread::isFinished ( ) const

Definition at line 58 of file qthread.cpp.

59 {
60  QMutexLocker locker(&d->mutex);
61  return d->finished;
62 }
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
bool QThread::isRunning ( ) const

Definition at line 64 of file qthread.cpp.

65 {
66  QMutexLocker locker(&d->mutex);
67  return d->running;
68 }
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
virtual void QThread::run ( )
inlineprotectedvirtual

Reimplemented in DotWorkerThread.

Definition at line 70 of file qthread.h.

70 {}
void QThread::setStackSize ( unsigned int  stackSize)

Definition at line 70 of file qthread.cpp.

71 {
72  QMutexLocker locker(&d->mutex);
73  if (d->running)
74  {
75  qWarning("QThread: Cannot change stack size while thread is running!");
76  return;
77  }
79 }
unsigned int stackSize() const
Definition: qthread.cpp:81
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
uint stackSize
Definition: qthread_p.h:68
unsigned int QThread::stackSize ( ) const

Definition at line 81 of file qthread.cpp.

82 {
83  QMutexLocker locker(&d->mutex);
84  return d->stackSize;
85 }
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
uint stackSize
Definition: qthread_p.h:68
void QThread::start ( )

Definition at line 117 of file qthread_unix.cpp.

118 {
119  QMutexLocker locker(&d->mutex);
120  if (d->running) return;
121 
122  // Block the SIGINT signal. The threads will inherit the signal mask.
123  // This will avoid them catching SIGINT instead of this thread.
124  sigset_t sigset, oldset;
125  sigemptyset(&sigset);
126  sigaddset(&sigset, SIGINT);
127  pthread_sigmask(SIG_BLOCK, &sigset, &oldset);
128 
129  d->running = TRUE;
130  d->finished = FALSE;
131 
132  pthread_attr_t attr;
133  pthread_attr_init(&attr);
134  pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
135  pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
136  if (d->stackSize>0)
137  {
138 #if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0>0)
139  pthread_attr_setstacksize(&attr,d->stackSize);
140 #endif
141  }
142  int code = pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this);
143  pthread_attr_destroy(&attr);
144 
145  if (code)
146  {
147  qWarning("QThread::start: Thread creation error: %d", code);
148 
149  d->running = FALSE;
150  d->finished = FALSE;
151  d->thread_id = 0;
152  }
153  else
154  {
155  // Restore the old signal mask only for this thread.
156  pthread_sigmask(SIG_SETMASK, &oldset, NULL);
157  }
158 }
static void * start(void *arg)
pthread_t thread_id
Definition: qthread_p.h:71
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
CodeOutputInterface * code
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
uint stackSize
Definition: qthread_p.h:68
const bool TRUE
Definition: qglobal.h:371
virtual void QThread::started ( )
inlineprotectedvirtual

Definition at line 65 of file qthread.h.

65 {}
void QThread::terminate ( )

Definition at line 160 of file qthread_unix.cpp.

161 {
162  QMutexLocker locker(&d->mutex);
163 
164  if (!d->thread_id) return;
165 
166  int code = pthread_cancel(d->thread_id);
167  if (code)
168  {
169  qWarning("QThread::start: Thread termination error: %d", code);
170  }
171  else
172  {
173  d->terminated = TRUE;
174  }
175 }
pthread_t thread_id
Definition: qthread_p.h:71
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
CodeOutputInterface * code
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
const bool TRUE
Definition: qglobal.h:371
bool terminated
Definition: qthread_p.h:67
virtual void QThread::terminated ( )
inlineprotectedvirtual

Definition at line 67 of file qthread.h.

67 {}
void QThread::wait ( )

Definition at line 177 of file qthread_unix.cpp.

178 {
179  QMutexLocker locker(&d->mutex);
180  if (d->finished || !d->running) return;
181 
182  while (d->running)
183  {
184  d->thread_done.wait(locker.mutex());
185  }
186 }
QWaitCondition thread_done
Definition: qthread_p.h:72
QThreadPrivate * d
Definition: qthread.h:73
QMutex mutex
Definition: qthread_p.h:63
void wait(QMutex *mutex)

Friends And Related Function Documentation

friend class QThreadPrivate
friend

Definition at line 74 of file qthread.h.

Member Data Documentation

QThreadPrivate* QThread::d
private

Definition at line 73 of file qthread.h.


The documentation for this class was generated from the following files: