18 #ifndef INCLUDE_RCF_THREADMANAGER_HPP 19 #define INCLUDE_RCF_THREADMANAGER_HPP 28 #include <RCF/AsioFwd.hpp> 29 #include <RCF/Export.hpp> 30 #include <RCF/ThreadLibrary.hpp> 31 #include <RCF/Timer.hpp> 36 typedef std::function<void(int)> Task;
38 typedef std::function<void()> StopFunctor;
41 typedef std::shared_ptr<ThreadPool> ThreadPoolPtr;
45 typedef std::shared_ptr<ThreadPool> ThreadPoolPtr;
48 class RCF_EXPORT ThreadInfo
51 ThreadInfo(ThreadPool & threadPool);
54 ThreadPool & getThreadPool();
57 friend class ThreadPool;
58 friend class ShouldStop;
60 ThreadPool & mThreadPool;
62 bool mStopFlag =
false;
63 bool mAlreadyRemovedFromThreadPool =
false;
64 RCF::Timer mTouchTimer;
67 typedef std::shared_ptr<ThreadInfo> ThreadInfoPtr;
75 static const MuxerType DefaultMuxerType = Mt_Asio;
79 public std::enable_shared_from_this<ThreadPool>
83 typedef std::function<void()> ThreadInitFunctor;
84 typedef std::function<void()> ThreadDeinitFunctor;
89 ThreadPool(std::size_t threadMinCount, std::size_t threadMaxCount);
92 void setThreadMinCount(std::size_t threadMinCount);
95 std::size_t getThreadMinCount()
const;
98 void setThreadMaxCount(std::size_t threadMaxCount);
101 std::size_t getThreadMaxCount()
const;
106 void setThreadIdleTimeoutMs(std::uint32_t threadIdleTimeoutMs);
109 std::uint32_t getThreadIdleTimeoutMs()
const;
114 void setReserveLastThread(
bool reserveLastThread);
115 bool getReserveLastThread()
const;
118 void setThreadName(
const std::string & threadName);
121 std::string getThreadName()
const;
133 void addThreadInitFunctor(
134 ThreadInitFunctor threadInitFunctor);
137 void addThreadDeinitFunctor(
138 ThreadDeinitFunctor threadDeinitFunctor);
140 AsioIoService * getIoService();
144 std::size_t getThreadCount();
146 void setTask(Task task);
147 void setStopFunctor(StopFunctor stopFunctor);
149 void enableMuxerType(MuxerType muxerType);
153 bool shouldStop()
const;
159 void setMyThreadName();
161 bool launchThread(std::size_t howManyThreads = 1);
166 RCF::ThreadInfoPtr threadInfoPtr,
169 void cycle(
int timeoutMs, ShouldStop & shouldStop);
171 friend class TaskEntry;
174 mutable Mutex mInitDeinitMutex;
175 std::vector<ThreadInitFunctor> mThreadInitFunctors;
176 std::vector<ThreadDeinitFunctor> mThreadDeinitFunctors;
177 std::string mThreadName;
178 std::shared_ptr<AsioMuxer> mAsioIoServicePtr;
181 std::size_t mThreadMinCount;
182 std::size_t mThreadMaxCount;
183 bool mReserveLastThread;
184 std::uint32_t mThreadIdleTimeoutMs;
187 StopFunctor mStopFunctor;
191 typedef std::map<ThreadInfoPtr, ThreadPtr> ThreadMap;
195 std::size_t mBusyCount;
196 Condition mAllThreadsStopped;
199 class ThreadTouchGuard
205 ThreadInfoPtr mThreadInfoPtr;
213 ThreadInfoPtr threadInfoPtr);
215 bool operator()()
const;
220 ThreadInfoPtr mThreadInfoPtr;
223 RCF_EXPORT
void setWin32ThreadName(
const std::string & threadName);
227 #endif // ! INCLUDE_RCF_THREADMANAGER_HPP Provides RCF server-side functionality.
Definition: RcfServer.hpp:53
Represents a server-side thread pool.
Definition: ThreadPool.hpp:78
Definition: AmiIoHandler.hpp:23