50 inline bool operator<(int priority, const QPair<QRunnable *, int> &p)
52 return p.second < priority;
54 inline bool operator<(const QPair<QRunnable *, int> &p,
int priority)
56 return priority < p.second;
69 void registerThreadInactive();
85 :manager(manager), runnable(0)
105 #ifndef QT_NO_EXCEPTIONS 109 #ifndef QT_NO_EXCEPTIONS 111 qWarning(
"Qt Concurrent has caught an exception thrown from a worker thread.\n" 112 "This is not supported, exceptions thrown in worker threads must be\n" 113 "caught before control returns to Qt Concurrent.");
120 if (autoDelete && !--r->
ref)
167 expiryTimeout(30000),
304 ((t = msecs - timer.
elapsed()) > 0))
324 const bool autoDelete = runnable->
autoDelete();
325 bool del = autoDelete && !--runnable->
ref;
358 if (it->
first == runnable) {
370 const bool autoDelete = runnable->
autoDelete();
371 bool del = autoDelete && !--runnable->
ref;
449 d_func()->waitForDone();
458 return theInstance();
483 if (!
d->tryStart(runnable)) {
484 d->enqueueTask(runnable, priority);
486 if (!
d->waitingThreads.isEmpty())
487 d->waitingThreads.takeFirst()->runnableReady.wakeOne();
516 if (
d->allThreads.isEmpty() ==
false &&
d->activeThreadCount() >=
d->maxThreadCount)
519 return d->tryStart(runnable);
542 return d->expiryTimeout;
548 if (
d->expiryTimeout == expiryTimeout)
570 return d->maxThreadCount;
578 if (maxThreadCount ==
d->maxThreadCount)
582 d->tryToStartMoreThreads();
602 return d->activeThreadCount();
621 ++
d->reservedThreads;
640 --
d->reservedThreads;
641 d->tryToStartMoreThreads();
668 bool rc =
d->waitForDone(msecs);
void releaseThread()
Releases a thread previously reserved by a call to reserveThread().
Q_OUTOFLINE_TEMPLATE RandomAccessIterator qUpperBound(RandomAccessIterator begin, RandomAccessIterator end, const T &value)
bool tooManyThreadsActive() const
QThreadPoolThread(QThreadPoolPrivate *manager)
int activeThreadCount() const
#define QT_END_NAMESPACE
This macro expands to.
T * data() const
Returns the value of the pointer referenced by this object.
#define it(className, varName)
void registerThreadInactive()
#define at(className, varName)
T * take()
Returns the value of the pointer referenced by this object.
void unlock()
Unlocks this mutex locker.
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
void insert(int i, const T &t)
Inserts value at index position i in the list.
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
int count(const T &t) const
Returns the number of occurrences of value in the list.
Q_DECL_CONSTEXPR T qAbs(const T &t)
The QObject class is the base class of all Qt objects.
virtual void run()=0
Implement this pure virtual function in your subclass.
The QElapsedTimer class provides a fast way to calculate elapsed times.
The QRunnable class is the base class for all runnable objects.
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
void stealRunnable(QRunnable *)
Searches for runnable in the queue, removes it from the queue and runs it if found.
The QScopedPointer class stores a pointer to a dynamically allocated object, and deletes it upon dest...
void setObjectName(const QString &name)
void relock()
Relocks an unlocked mutex locker.
bool removeOne(const T &t)
Removes the first occurrence of value in the list and returns true on success; otherwise returns fals...
The QThreadPool class manages a collection of QThreads.
#define QT_BEGIN_NAMESPACE
This macro expands to.
QQueue< QThreadPoolThread * > expiredThreads
bool tryStart(QRunnable *runnable)
Attempts to reserve a thread to run runnable.
void waitForDone()
Waits for each thread to exit and removes all threads from the thread pool.
T takeFirst()
Removes the first item in the list and returns it.
friend class QThreadPoolThread
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
int expiryTimeout() const
void start(QRunnable *runnable, int priority=0)
Reserves a thread and uses it to run runnable, unless this thread will make the current thread count ...
Q_CORE_EXPORT void qWarning(const char *,...)
const_iterator insert(const T &value)
QThreadPoolPrivate * manager
void removeFirst()
Removes the first item in the list.
~QThreadPool()
Destroys the QThreadPool.
void run()
The starting point for the thread.
void clear()
Removes all items from the list.
void reset()
Makes all threads exit, waits for each thread to exit and deletes it.
QSet< QThreadPoolThread * > allThreads
bool wait(QMutex *mutex, unsigned long time=ULONG_MAX)
void setExpiryTimeout(int expiryTimeout)
T & first()
Returns a reference to the first item in the list.
void enqueue(const T &t)
Adds value t to the tail of the queue.
iterator erase(iterator pos)
Removes the item associated with the iterator pos from the list, and returns an iterator to the next ...
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
int maxThreadCount() const
void start(Priority=InheritPriority)
Begins execution of the thread by calling run().
bool autoDelete() const
Returns true is auto-deletion is enabled; false otherwise.
bool wait(unsigned long time=ULONG_MAX)
Blocks the thread until either of these conditions is met:
Q_OUTOFLINE_TEMPLATE QPair< T1, T2 > qMakePair(const T1 &x, const T2 &y)
void tryToStartMoreThreads()
QWaitCondition noActiveThreads
static QThreadPool * globalInstance()
Returns the global QThreadPool instance.
QFuture< T > run(Function function,...)
bool startFrontRunnable()
Pulls a runnable from the front queue and runs it in the current thread.
int activeThreadCount() const
void enqueueTask(QRunnable *task, int priority=0)
QWaitCondition runnableReady
QThreadPool(QObject *parent=0)
Constructs a thread pool with the given parent.
T dequeue()
Removes the head item in the queue and returns it.
void startThread(QRunnable *runnable=0)
static const KeyPair *const end
QQueue< QThreadPoolThread * > waitingThreads
The QThread class provides a platform-independent way to manage threads.
void setMaxThreadCount(int maxThreadCount)
void start()
Starts this timer.
QMutex * mutex() const
Returns a pointer to the mutex that was locked in the constructor.
QList< QPair< QRunnable *, int > > queue
void reserveThread()
Reserves one thread, disregarding activeThreadCount() and maxThreadCount().
The QList class is a template class that provides lists.
bool tryStart(QRunnable *task)
bool waitForDone(int msecs=-1)
static int idealThreadCount()
Returns the ideal number of threads that can be run on the system.