This commit is contained in:
azw 2023-05-07 01:55:19 +00:00
parent 9dc08638a0
commit e41ffbd58f
2 changed files with 17 additions and 3 deletions

View File

@ -8,6 +8,9 @@ void IoMgr::Init()
{
for (int i = 0; i < IC_Max; ++i) {
io_contexts_.push_back(std::vector<std::shared_ptr<asio::io_context>>());
auto& contexts = io_contexts_.at(i);
contexts.push_back(std::make_shared<asio::io_context>());
new std::thread(&IoMgr::WorkerThreadProc, this, contexts.at(0));
}
}
@ -19,12 +22,20 @@ void IoMgr::UnInit()
std::shared_ptr<asio::io_context> IoMgr::GetIoContext(int type)
{
if (type >= 0 && type < io_contexts_.size()) {
auto& s = io_contexts_.at(type);
if (s.empty()) {
return s.at(rand() % s.size());
auto& contexts = io_contexts_.at(type);
if (contexts.empty()) {
return contexts.at(rand() % contexts.size());
}
}
return nullptr;
}
void IoMgr::WorkerThreadProc(std::shared_ptr<asio::io_context> io_context)
{
while (true) {
io_context->run();
io_context->reset();
}
}
#endif

View File

@ -17,6 +17,9 @@ public:
std::shared_ptr<asio::io_context> GetIoContext(int type);
private:
void WorkerThreadProc(std::shared_ptr<asio::io_context> io_context);
private:
std::vector<std::vector<std::shared_ptr<asio::io_context>>> io_contexts_;