fix concurrent

git-svn-id: svn://db.shs.com.ru/pip@884 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
2020-02-25 15:58:02 +00:00
parent f8f627360a
commit 92ac2b12cf
7 changed files with 408 additions and 467 deletions

View File

@@ -5,46 +5,8 @@
#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
@@ -52,36 +14,35 @@ public:
*/
class PIThreadPoolExecutor {
public:
explicit PIThreadPoolExecutor(size_t corePoolSize = 1, PIBlockingDequeue<std::function<void()> >* taskQueue_ = new PIBlockingDequeue<std::function<void()> >(), PIThreadFactory* threadFactory = new PIThreadFactory());
explicit PIThreadPoolExecutor(size_t corePoolSize = 1, PIBlockingDequeue<std::function<void()> >* taskQueue_ = new PIBlockingDequeue<std::function<void()> >());
virtual ~PIThreadPoolExecutor();
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);
/**
* @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();
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();
/**
* @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();
volatile bool isShutdown() const;
volatile bool isShutdown() const;
bool awaitTermination(int timeoutMs);
bool awaitTermination(int timeoutMs);
private:
volatile bool isShutdown_;
PIBlockingDequeue<std::function<void()> >* taskQueue;
PIThreadFactory* threadFactory;
PIVector<AbstractThread*> threadPool;
volatile bool isShutdown_;
PIBlockingDequeue<std::function<void()> >* taskQueue;
PIVector<PIThread*> threadPool;
};
#endif //PIP_TESTS_EXECUTOR_H