Skip to content

Commit

Permalink
wrap task call in exception handler
Browse files Browse the repository at this point in the history
- add some more stats and improve stats with exception counter
  • Loading branch information
astibal committed Jun 28, 2024
1 parent ee95307 commit 4dcd590
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/service/tpool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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--;
}
});
Expand All @@ -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<class F>
Expand Down

0 comments on commit 4dcd590

Please sign in to comment.