From 16f66321a87bb29e512a52fd1b222d611363b2e5 Mon Sep 17 00:00:00 2001 From: sha512sum Date: Wed, 10 Apr 2024 16:45:29 +0000 Subject: [PATCH] Update init benchmark code --- benchmarks/init.cpp | 1 + include/cserver/engine/basic/task_processor.hpp | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/benchmarks/init.cpp b/benchmarks/init.cpp index 9b29413..48bf505 100644 --- a/benchmarks/init.cpp +++ b/benchmarks/init.cpp @@ -87,6 +87,7 @@ auto BMInitialise(benchmark::State& state) { for(auto _ : state) { auto* ptr = new (context) ServiceContextType{}; ptr->Run(); + ptr->FindComponent().ioContext.run(); state.PauseTiming(); ptr->~ServiceContextType(); state.ResumeTiming(); diff --git a/include/cserver/engine/basic/task_processor.hpp b/include/cserver/engine/basic/task_processor.hpp index d67d96d..4d69dad 100644 --- a/include/cserver/engine/basic/task_processor.hpp +++ b/include/cserver/engine/basic/task_processor.hpp @@ -1,13 +1,14 @@ #pragma once #include #include +#include namespace cserver::engine::basic { template struct TaskProcessor { boost::asio::io_context ioContext; - std::array pool; + std::array, Size> pool; static constexpr utempl::ConstexprString kName = "basicTaskProcessor"; inline constexpr TaskProcessor(auto, auto&) : ioContext{}, @@ -15,16 +16,22 @@ struct TaskProcessor { }; inline constexpr ~TaskProcessor() { - this->ioContext.stop(); for(auto& thread : this->pool) { - thread.join(); + if(thread && thread->joinable()) { + thread->join(); + }; }; }; inline auto Run() { for(auto& thread : this->pool) { - thread = std::thread([&]{ - boost::asio::executor_work_guardioContext.get_executor())> guard{this->ioContext.get_executor()}; + if(this->ioContext.stopped()) { + return; + }; + thread = std::jthread([&]{ + if(this->ioContext.stopped()) { + return; + }; this->ioContext.run(); }); };