SpiecsEngine
 
Loading...
Searching...
No Matches
EventLoopThreadPool.cpp
Go to the documentation of this file.
1/**
2* @file EventLoopThreadPool.cpp.
3* @brief The EventLoopThreadPool Class Implementation.
4* @author Spices & Muduo.
5*/
6
7#include "Pchheader.h"
9
10namespace Spices {
11
12namespace Net {
13
15 {
17
19
20 if (!m_Loops.empty())
21 {
22 loop = m_Loops[m_Next];
23 ++m_Next;
24
25 if (m_Next >= m_Loops.size())
26 {
27 m_Next = 0;
28 }
29 }
30
31 return loop;
32 }
33
35 {
37
38 if (m_Loops.empty())
39 {
40 return std::vector<EventLoop*>(1, EventLoopThreadWrapper::GetInst());
41 }
42 else
43 {
44 return m_Loops;
45 }
46 }
47
49 {
51
52 /**
53 * @brief Name thread.
54 */
55 std::stringstream ss;
56 ss << m_PoolName << thread->GetId();
57
59
60 EventLoop* loop = EventLoopThreadWrapper::GetInst(&m_ListenAddress);
61
62 if (m_ThreadInitCallback)
63 {
64 m_ThreadInitCallback(loop);
65 }
66
67 m_Loops[thread->GetId()] = loop;
68 m_IsThreadsPrepared.Increase();
69
70 loop->Loop();
71 }
72
73 void EventLoopThreadPool::Start(int initThreadSize, ThreadInitCallback cb)
74 {
76
77 m_ThreadInitCallback = std::move(cb);
78 m_Loops.resize(initThreadSize);
79
80 m_IsPoolRunning = true;
81 m_InitThreadSize = initThreadSize;
82 m_IdleThreadSize = initThreadSize;
83 m_NThreads = initThreadSize;
84
85 for (uint32_t i = 0; i < m_InitThreadSize; i++)
86 {
87 auto ptr = std::make_unique<Thread<>>(std::bind(&EventLoopThreadPool::ThreadFunc, this, std::placeholders::_1), i);
88 int threadId = ptr->GetId();
89
90 m_Threads.emplace(threadId, std::move(ptr));
91 m_Threads[threadId]->Start();
92 }
93
94 /**
95 * @brief Wait for Thread EventLoop prepared.
96 */
97 m_IsThreadsPrepared.Wait(initThreadSize);
98
99 }
100
101}
102
103}
#define SPICES_PROFILE_ZONE
EventLoop * GetNextLoop()
Get next thread EventLoop.
std::vector< EventLoop * > GetAllLoops()
Get all thread EventLoop.
void ThreadFunc(Thread<> *thread)
Thread Function.
void Start(int initThreadSize=3, ThreadInitCallback cb=nullptr)
Start Run this thread pool.
ThreadPool of Multiple threading EventLoop.
static EventLoop *& GetInst(InetAddress *address=nullptr)
Get EventLoop Instance. @reutrn Returns EventLoop Instance.
Wrapper of Instance/Delete ThreadCache in thread.
Definition EventLoop.h:180
void Loop()
Start Event Loop.
Definition EventLoop.cpp:48
Wrapper of Poller and wakeup socket to acceptor(SubLoop).
Definition EventLoop.h:28
static bool SetThreadName(const std::string &name)
Set Thread name.
Thread Static Function Library.
Thread Function Object.