From 4dcd5903da8a746388307808260cb45cea4fe439 Mon Sep 17 00:00:00 2001 From: ales stibal Date: Fri, 28 Jun 2024 09:53:54 +0200 Subject: [PATCH] wrap task call in exception handler - add some more stats and improve stats with exception counter --- src/service/tpool.hpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/service/tpool.hpp b/src/service/tpool.hpp index ccf1811..414abe1 100644 --- a/src/service/tpool.hpp +++ b/src/service/tpool.hpp @@ -86,7 +86,14 @@ class ThreadPool { std::atomic_uint active = 0; + struct stats_t { + std::atomic_uint std_except = 0; + std::atomic_uint unk_except = 0; + }; + stats_t stats_; + public: + stats_t const& stats() const { return stats_; } // advice/recommendation for longer task to schedule their loops to check stop flag (if any) static constexpr unsigned long milliseconds = 100; @@ -114,7 +121,16 @@ class ThreadPool { } this->active++; - task(this->stop_); + try { + // RUN THE TASK + task(this->stop_); + } + catch(std::exception const&) { + this->stats_.std_except++; + } + catch(...) { + this->stats_.unk_except++; + } this->active--; } }); @@ -139,7 +155,9 @@ class ThreadPool { void stop() { stop_ = true; } void start() { stop_ = false; } - [[nodiscard]] bool ready() const { return stop_; } + [[nodiscard]] bool is_active() const { return (! stop_); } + [[nodiscard]] bool is_stopping() const { return stop_; } + void ready(bool b) { stop_ = b ; } template