sptk2 logo
SPTK Home Page
ThreadPool.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include <sptk5/LogEngine.h>
30#include <sptk5/threads/Runable.h>
31#include <sptk5/threads/SynchronizedList.h>
32#include <sptk5/threads/SynchronizedQueue.h>
33#include <sptk5/threads/ThreadEvent.h>
34#include <sptk5/threads/ThreadManager.h>
35#include <sptk5/threads/WorkerThread.h>
36
37namespace sptk {
38
52class SP_EXPORT ThreadPool
53 : public ThreadEvent
54 , public std::mutex
55{
56public:
63 ThreadPool(uint32_t threadLimit, std::chrono::milliseconds threadIdleSeconds, const String& threadName,
64 LogEngine* logEngine);
65
70 virtual void execute(const SRunable& task);
71
80 void threadEvent(Thread* thread, ThreadEvent::Type eventType, SRunable runable) override;
81
87 virtual void stop();
88
92 size_t size() const;
93
94private:
95 SThreadManager m_threadManager;
96 size_t m_threadLimit;
97 SynchronizedQueue<SRunable> m_taskQueue;
98 Semaphore m_availableThreads;
99 std::chrono::milliseconds m_threadIdleTime;
100 SLogger m_logger;
101
102 std::atomic_bool m_shutdown {false};
103
109 WorkerThread* createThread();
110
116 void logThreadEvent(const String& event, const Thread* workerThread) const;
117};
118
122} // namespace sptk
Definition: LogEngine.h:60
Generic unnamed semaphore class.
Definition: Semaphore.h:52
Definition: String.h:49
Thread event interface.
Definition: ThreadEvent.h:43
Type
Thread event type.
Definition: ThreadEvent.h:49
Definition: ThreadPool.h:55
virtual void stop()
virtual void execute(const SRunable &task)
void threadEvent(Thread *thread, ThreadEvent::Type eventType, SRunable runable) override
size_t size() const
ThreadPool(uint32_t threadLimit, std::chrono::milliseconds threadIdleSeconds, const String &threadName, LogEngine *logEngine)
Definition: Thread.h:51
Definition: WorkerThread.h:55

Fri Oct 14 2022 09:58:32: SPTK 5.4.1