5 #ifndef JODA_IOTHREADPOOL_H
6 #define JODA_IOTHREADPOOL_H
17 template <
class Worker>
23 typedef typename Worker::IQueue
IQueue;
24 typedef typename Worker::OQueue
OQueue;
27 typedef typename Worker::WConf
WConf;
49 if (!threads.back()->isRunning())
break;
69 if (!threads.back()->isRunning())
break;
92 for (
const auto& thread : threads) {
93 if (!thread->requestedToStop()) {
104 for (
auto& thread : threads) {
129 return Worker::recommendedThreads();
137 for (
const auto& item : threads) {
138 if (item->running())
return true;
144 IOThreadPool::workerConfig = workerConfig;
149 std::vector<std::unique_ptr<Worker>> threads{};
154 void clearThreads() {
155 threads.erase(std::remove_if(threads.begin(), threads.end(),
156 [](
const std::unique_ptr<Worker>& o) {
157 return !o->isRunning();
161 void removeThread() {
163 this->threads.back()->stop();
168 this->threads.emplace_back(
169 std::make_unique<Worker>(iqueue, oqueue, workerConfig));
170 this->threads.back()->start();
181 template <
class IQueueStruct,
class OQueueStruct,
class WConfig>
188 typedef typename IQueueStruct::queue_t
IQueue;
189 typedef typename OQueueStruct::queue_t
OQueue;
209 fut = std::async(std::launch::async, [
this] {
return this->run(); });
230 std::future<void> fut;
Definition: IOThreadPool.h:18
virtual ~IOThreadPool()=default
size_t recommendedThreads() const override
Definition: IOThreadPool.h:128
bool hasRunningThreads()
Definition: IOThreadPool.h:136
size_t getUsedThreads() const override
Definition: IOThreadPool.h:90
size_t getMaxThreads() const override
Definition: IOThreadPool.h:114
IOThreadPool(IQueue *iqueue, OQueue *oqueue, size_t maxThreads, WConf conf)
Definition: IOThreadPool.h:61
void setWorkerConfig(const WConf &workerConfig)
Definition: IOThreadPool.h:143
IOThreadPool(IQueue *iqueue, OQueue *oqueue, WConf conf)
Definition: IOThreadPool.h:42
void forceThreads(size_t threads) override
Definition: IOThreadPool.h:77
Worker::WConf WConf
Definition: IOThreadPool.h:27
void setMaxThreads(size_t maxThreads) override
Definition: IOThreadPool.h:120
Worker::OPayload OPayload
Definition: IOThreadPool.h:26
Worker::OQueue OQueue
Definition: IOThreadPool.h:24
void wait()
Definition: IOThreadPool.h:103
Worker::IPayload IPayload
Definition: IOThreadPool.h:25
Worker::IQueue IQueue
Definition: IOThreadPool.h:23
Definition: IThreadUser.h:13
virtual void setMaxThreads(size_t maxThreads)
Definition: IThreadUser.h:23
virtual size_t getMaxThreads() const
Definition: IThreadUser.h:21
size_t maxThreads
Definition: IThreadUser.h:30
Definition: IOThreadPool.h:182
IQueue * iqueue
Definition: IOThreadPool.h:218
IWorkerThread(IQueue *iqueue, OQueue *oqueue, WConf &&conf)
Definition: IOThreadPool.h:200
bool shouldRun
Definition: IOThreadPool.h:221
static constexpr auto getOQueueFlags()
Definition: IOThreadPool.h:196
static constexpr auto getIQueueFlags()
Definition: IOThreadPool.h:194
IWorkerThread(IWorkerThread &&)=default
OQueue * oqueue
Definition: IOThreadPool.h:219
void stop()
Definition: IOThreadPool.h:212
static const size_t recommendedThreads()
Definition: IOThreadPool.h:215
WConf conf
Definition: IOThreadPool.h:220
IWorkerThread & operator=(const IWorkerThread &)=delete
void waitFor()
Definition: IOThreadPool.h:211
WConfig WConf
Definition: IOThreadPool.h:192
bool requestedToStop() const
Definition: IOThreadPool.h:213
OQueueStruct::queue_t OQueue
Definition: IOThreadPool.h:189
IWorkerThread(const IWorkerThread &)=delete
OQueueStruct::payload_t OPayload
Definition: IOThreadPool.h:191
IWorkerThread(IQueue *iqueue, OQueue *oqueue, const WConf &conf)
Definition: IOThreadPool.h:198
virtual ~IWorkerThread()
Definition: IOThreadPool.h:206
void start()
Definition: IOThreadPool.h:208
IQueueStruct::queue_t IQueue
Definition: IOThreadPool.h:188
bool isRunning() const
Definition: IOThreadPool.h:214
IQueueStruct::payload_t IPayload
Definition: IOThreadPool.h:190
IWorkerThread & operator=(IWorkerThread &&)=default