42 #ifndef QTCONCURRENT_THREADENGINE_H 43 #define QTCONCURRENT_THREADENGINE_H 45 #include <QtCore/qglobal.h> 47 #ifndef QT_NO_CONCURRENT 49 #include <QtCore/qthreadpool.h> 50 #include <QtCore/qfuture.h> 51 #include <QtCore/qdebug.h> 52 #include <QtCore/qtconcurrentexception.h> 53 #include <QtCore/qwaitcondition.h> 54 #include <QtCore/qatomic.h> 55 #include <QtCore/qsemaphore.h> 69 class ThreadEngineBarrier
83 ThreadEngineBarrier();
88 bool releaseUnlessLast();
91 enum ThreadFunctionResult { ThrottleThread, ThreadFinished };
102 virtual ~ThreadEngineBase();
103 void startSingleThreaded();
104 void startBlocking();
107 void waitForResume();
108 bool isProgressReportingEnabled();
109 void setProgressValue(
int progress);
110 void setProgressRange(
int minimum,
int maximum);
111 void acquireBarrierSemaphore();
114 virtual void start() {}
115 virtual void finish() {}
116 virtual ThreadFunctionResult threadFunction() {
return ThreadFinished; }
117 virtual bool shouldStartThread() {
return futureInterface ? !futureInterface->isPaused() :
true; }
118 virtual bool shouldThrottleThread() {
return futureInterface ? futureInterface->isPaused() :
false; }
120 bool startThreadInternal();
123 bool threadThrottleExit();
125 virtual void asynchronousFinish() = 0;
126 #ifndef QT_NO_EXCEPTIONS 132 ThreadEngineBarrier barrier;
137 template <
typename T>
138 class ThreadEngine :
public virtual ThreadEngineBase
141 typedef T ResultType;
143 virtual T *result() {
return 0; }
151 T *startSingleThreaded()
153 ThreadEngineBase::startSingleThreaded();
162 ThreadEngineBase::startBlocking();
179 acquireBarrierSemaphore();
180 threadPool->start(
this);
184 void asynchronousFinish()
187 futureInterfaceTyped()->reportFinished(result());
188 delete futureInterfaceTyped();
193 void reportResult(
const T *_result,
int index = -1)
196 futureInterfaceTyped()->reportResult(_result,
index);
199 void reportResults(
const QVector<T> &_result,
int index = -1,
int count = -1)
202 futureInterfaceTyped()->reportResults(_result,
index, count);
210 template <
typename T>
211 class ThreadEngineStarterBase
214 ThreadEngineStarterBase(ThreadEngine<T> *_threadEngine)
215 : threadEngine(_threadEngine) { }
217 inline ThreadEngineStarterBase(
const ThreadEngineStarterBase &other)
218 : threadEngine(other.threadEngine) { }
222 return threadEngine->startAsynchronously();
227 return startAsynchronously();
231 ThreadEngine<T> *threadEngine;
238 template <
typename T>
239 class ThreadEngineStarter :
public ThreadEngineStarterBase<T>
241 typedef ThreadEngineStarterBase<T> Base;
242 typedef ThreadEngine<T> TypedThreadEngine;
244 ThreadEngineStarter(TypedThreadEngine *eng)
249 T t = *this->threadEngine->startBlocking();
250 delete this->threadEngine;
257 class ThreadEngineStarter<void> :
public ThreadEngineStarterBase<void>
260 ThreadEngineStarter<void>(ThreadEngine<void> *_threadEngine)
261 :ThreadEngineStarterBase<void>(_threadEngine) {}
265 this->threadEngine->startBlocking();
266 delete this->threadEngine;
270 template <
typename ThreadEngine>
271 inline ThreadEngineStarter<typename ThreadEngine::ResultType> startThreadEngine(ThreadEngine *threadEngine)
273 return ThreadEngineStarter<typename ThreadEngine::ResultType>(threadEngine);
283 #endif // QT_NO_CONCURRENT #define QT_END_NAMESPACE
This macro expands to.
The QSemaphore class provides a general counting semaphore.
The QMutex class provides access serialization between threads.
The QAtomicInt class provides platform-independent atomic operations on integers. ...
The QVector class is a template class that provides a dynamic array.
The QRunnable class is the base class for all runnable objects.
The QThreadPool class manages a collection of QThreads.
#define QT_BEGIN_NAMESPACE
This macro expands to.
The QFuture class represents the result of an asynchronous computation.
The Exception class provides a base class for exceptions that can transferred across threads...
The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded pro...
QFuture< T > run(Function function,...)