Update init benchmark code

This commit is contained in:
sha512sum 2024-04-10 16:45:29 +00:00 committed by sha512sum
parent 9f48938386
commit 16f66321a8
2 changed files with 13 additions and 5 deletions

View file

@ -87,6 +87,7 @@ auto BMInitialise(benchmark::State& state) {
for(auto _ : state) {
auto* ptr = new (context) ServiceContextType{};
ptr->Run();
ptr->FindComponent<cserver::kBasicTaskProcessorName>().ioContext.run();
state.PauseTiming();
ptr->~ServiceContextType();
state.ResumeTiming();

View file

@ -1,13 +1,14 @@
#pragma once
#include <utempl/constexpr_string.hpp>
#include <boost/asio.hpp>
#include <thread>
namespace cserver::engine::basic {
template <std::size_t Size = 0>
struct TaskProcessor {
boost::asio::io_context ioContext;
std::array<std::thread, Size> pool;
std::array<std::optional<std::thread>, 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_guard<decltype(this->ioContext.get_executor())> guard{this->ioContext.get_executor()};
if(this->ioContext.stopped()) {
return;
};
thread = std::jthread([&]{
if(this->ioContext.stopped()) {
return;
};
this->ioContext.run();
});
};