Rewrite executor to template & come back executor unit tests
This commit is contained in:
@@ -27,12 +27,18 @@
|
||||
* @brief Thread pools address two different problems: they usually provide improved performance when executing large
|
||||
* numbers of asynchronous tasks, due to reduced per-task invocation overhead, and they provide a means of bounding and
|
||||
* managing the resources, including threads, consumed when executing a collection of tasks.
|
||||
*
|
||||
* TODO adapt documentation to template
|
||||
*/
|
||||
class PIThreadPoolExecutor {
|
||||
template <typename Thread_, typename Dequeue_>
|
||||
class PIThreadPoolExecutorTemplate {
|
||||
public:
|
||||
explicit PIThreadPoolExecutor(size_t corePoolSize = 1, PIBlockingDequeue<std::function<void()> >* taskQueue_ = new PIBlockingDequeue<std::function<void()> >());
|
||||
explicit PIThreadPoolExecutorTemplate(size_t corePoolSize = 1) : isShutdown_(false) { makePool(corePoolSize); }
|
||||
|
||||
virtual ~PIThreadPoolExecutor();
|
||||
virtual ~PIThreadPoolExecutorTemplate() {
|
||||
shutdownNow();
|
||||
while (threadPool.size() > 0) delete threadPool.take_back();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Executes the given task sometime in the future. The task execute in an existing pooled thread. If the task
|
||||
@@ -41,24 +47,65 @@ public:
|
||||
*
|
||||
* @param runnable not empty function for thread pool execution
|
||||
*/
|
||||
void execute(const std::function<void()>& runnable);
|
||||
|
||||
void shutdownNow();
|
||||
void execute(const std::function<void()> &runnable) {
|
||||
if (!isShutdown_) taskQueue.offer(runnable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be
|
||||
* accepted. Invocation has no additional effect if already shut down. This method does not wait for previously
|
||||
* submitted tasks to complete execution. Use awaitTermination to do that.
|
||||
*/
|
||||
void shutdown();
|
||||
void shutdown() {
|
||||
isShutdown_ = true;
|
||||
}
|
||||
|
||||
bool isShutdown() const;
|
||||
void shutdownNow() {
|
||||
isShutdown_ = true;
|
||||
for (size_t i = 0; i < threadPool.size(); ++i) threadPool[i]->stop();
|
||||
}
|
||||
|
||||
bool awaitTermination(int timeoutMs);
|
||||
private:
|
||||
bool isShutdown() const {
|
||||
return isShutdown_;
|
||||
}
|
||||
|
||||
bool awaitTermination(int timeoutMs) {
|
||||
PITimeMeasurer measurer;
|
||||
for (size_t i = 0; i < threadPool.size(); ++i) {
|
||||
int dif = timeoutMs - (int)measurer.elapsed_m();
|
||||
if (dif < 0) return false;
|
||||
if (!threadPool[i]->waitForFinish(dif)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
std::atomic_bool isShutdown_;
|
||||
PIBlockingDequeue<std::function<void()> >* taskQueue;
|
||||
PIVector<PIThread*> threadPool;
|
||||
Dequeue_ taskQueue;
|
||||
PIVector<Thread_*> threadPool;
|
||||
|
||||
template<typename Function>
|
||||
PIThreadPoolExecutorTemplate(size_t corePoolSize, Function onBeforeStart) : isShutdown_(false) {
|
||||
makePool(corePoolSize, onBeforeStart);
|
||||
}
|
||||
|
||||
void makePool(size_t corePoolSize, std::function<void(Thread_*)> onBeforeStart = [](Thread_*){}) {
|
||||
for (size_t i = 0; i < corePoolSize; ++i) {
|
||||
auto* thread = new Thread_([&, i](){
|
||||
auto runnable = taskQueue.poll(100);
|
||||
if (runnable) {
|
||||
runnable();
|
||||
}
|
||||
if (isShutdown_ && taskQueue.size() == 0) threadPool[i]->stop();
|
||||
});
|
||||
threadPool.push_back(thread);
|
||||
onBeforeStart(thread);
|
||||
thread->start();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
typedef PIThreadPoolExecutorTemplate<PIThread, PIBlockingDequeue<std::function<void()> > > PIThreadPoolExecutor;
|
||||
|
||||
|
||||
#endif //PIP_TESTS_EXECUTOR_H
|
||||
|
||||
Reference in New Issue
Block a user