Added inter thread communication lib
git-svn-id: svn://db.shs.com.ru/pip@858 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
89
src_main/concurrent/executor.h
Normal file
89
src_main/concurrent/executor.h
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// Created by fomenko on 20.09.2019.
|
||||
//
|
||||
|
||||
#ifndef PIP_TESTS_EXECUTOR_H
|
||||
#define PIP_TESTS_EXECUTOR_H
|
||||
|
||||
#include <pithread.h>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include "piblockingdequeue.h"
|
||||
|
||||
class AbstractThread {
|
||||
public:
|
||||
virtual bool start() = 0;
|
||||
virtual bool waitForStart(int timeout_msecs) = 0;
|
||||
virtual bool waitForFinish(int timeout_msecs) = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual ~AbstractThread() = default;
|
||||
};
|
||||
|
||||
class Thread : public AbstractThread {
|
||||
public:
|
||||
explicit Thread(const std::function<void()>& fun = [](){}) : adapter(fun) {
|
||||
adapter.registerToInvoke(&thread);
|
||||
}
|
||||
virtual ~Thread() = default;
|
||||
|
||||
inline bool start() override { return thread.start(); }
|
||||
inline bool waitForStart(int timeout_msecs) override { return thread.waitForStart(timeout_msecs); }
|
||||
inline bool waitForFinish(int timeout_msecs) override { return thread.waitForFinish(timeout_msecs); }
|
||||
inline void stop() override { thread.stop(); }
|
||||
|
||||
private:
|
||||
PIThread thread;
|
||||
StdFunctionThreadFuncAdapter adapter;
|
||||
};
|
||||
|
||||
class PIThreadFactory {
|
||||
public:
|
||||
inline virtual AbstractThread* newThread(const std::function<void()>& fun) {
|
||||
return new Thread(fun);
|
||||
}
|
||||
virtual ~PIThreadFactory() = default;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
class PIThreadPoolExecutor {
|
||||
public:
|
||||
explicit PIThreadPoolExecutor(size_t corePoolSize = 1, PIBlockingDequeue<std::function<void()> >* taskQueue_ = new PIBlockingDequeue<std::function<void()> >(), PIThreadFactory* threadFactory = new PIThreadFactory());
|
||||
|
||||
virtual ~PIThreadPoolExecutor();
|
||||
|
||||
/**
|
||||
* @brief Executes the given task sometime in the future. The task execute in an existing pooled thread. If the task
|
||||
* cannot be submitted for execution, either because this executor has been shutdown or because its capacity has been
|
||||
* reached.
|
||||
*
|
||||
* @param runnable not empty function for thread pool execution
|
||||
*/
|
||||
void execute(const std::function<void()>& runnable);
|
||||
|
||||
void shutdownNow();
|
||||
|
||||
/**
|
||||
* @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() {
|
||||
isShutdown_ = true;
|
||||
}
|
||||
|
||||
volatile bool isShutdown() const;
|
||||
|
||||
bool awaitTermination(int timeoutMs);
|
||||
private:
|
||||
volatile bool isShutdown_;
|
||||
PIBlockingDequeue<std::function<void()> >* taskQueue;
|
||||
PIThreadFactory* threadFactory;
|
||||
PIVector<AbstractThread*> threadPool;
|
||||
};
|
||||
|
||||
#endif //PIP_TESTS_EXECUTOR_H
|
||||
Reference in New Issue
Block a user