185class FastThreadPoolWithErrors
188 explicit FastThreadPoolWithErrors(
size_t num_threads = std::thread::hardware_concurrency())
189 : pool_(num_threads), error_handler_(std::make_shared<ErrorHandler>())
193 template <
typename F,
typename... Args>
196 auto handler = error_handler_;
197 auto wrapped_task = [f = std::forward<F>(f), args = std::make_tuple(std::forward<Args>(args)...), handler]() {
200 return std::apply(f, args);
205 error.exception = std::current_exception();
206 error.thread_id = std::this_thread::get_id();
207 error.timestamp = std::chrono::steady_clock::now();
208 handler->handle_error(error);
213 auto future = pool_.submit(std::move(wrapped_task));
217 template <
typename F,
typename... Args>
218 auto submit_with_description(std::string
const& description, F&& f, Args&&... args)
221 auto handler = error_handler_;
222 auto wrapped_task = [f = std::forward<F>(f), args = std::make_tuple(std::forward<Args>(args)...), handler,
226 return std::apply(f, args);
231 error.exception = std::current_exception();
232 error.task_description = description;
233 error.thread_id = std::this_thread::get_id();
234 error.timestamp = std::chrono::steady_clock::now();
235 handler->handle_error(error);
240 auto future = pool_.submit(std::move(wrapped_task));
244 auto add_error_callback(ErrorCallback callback) ->
size_t
246 return error_handler_->add_callback(std::move(callback));
249 void clear_error_callbacks()
251 error_handler_->clear_callbacks();
254 [[nodiscard]]
auto error_count()
const ->
size_t
256 return error_handler_->error_count();
259 void reset_error_count()
261 error_handler_->reset_error_count();
271 return pool_.get_statistics();
274 auto configure_threads(std::string
const& name_prefix, SchedulingPolicy policy = SchedulingPolicy::OTHER,
277 return pool_.configure_threads(name_prefix, policy, priority);
280 auto distribute_across_cpus() ->
bool
282 return pool_.distribute_across_cpus();
290 [[nodiscard]]
auto size()
const noexcept ->
size_t
295 [[nodiscard]]
auto pending_tasks()
const ->
size_t
297 return pool_.pending_tasks();
302 std::shared_ptr<ErrorHandler> error_handler_;
308class ThreadPoolWithErrors
311 explicit ThreadPoolWithErrors(
size_t num_threads = std::thread::hardware_concurrency())
312 : pool_(num_threads), error_handler_(std::make_shared<ErrorHandler>())
316 template <
typename F,
typename... Args>
319 auto handler = error_handler_;
320 auto wrapped_task = [f = std::forward<F>(f), args = std::make_tuple(std::forward<Args>(args)...), handler]() {
323 return std::apply(f, args);
328 error.exception = std::current_exception();
329 error.thread_id = std::this_thread::get_id();
330 error.timestamp = std::chrono::steady_clock::now();
331 handler->handle_error(error);
336 auto future = pool_.submit(std::move(wrapped_task));
340 template <
typename F,
typename... Args>
341 auto submit_with_description(std::string
const& description, F&& f, Args&&... args)
344 auto handler = error_handler_;
345 auto wrapped_task = [f = std::forward<F>(f), args = std::make_tuple(std::forward<Args>(args)...), handler,
349 return std::apply(f, args);
354 error.exception = std::current_exception();
355 error.task_description = description;
356 error.thread_id = std::this_thread::get_id();
357 error.timestamp = std::chrono::steady_clock::now();
358 handler->handle_error(error);
363 auto future = pool_.submit(std::move(wrapped_task));
367 auto add_error_callback(ErrorCallback callback) ->
size_t
369 return error_handler_->add_callback(std::move(callback));
372 void clear_error_callbacks()
374 error_handler_->clear_callbacks();
377 [[nodiscard]]
auto error_count()
const ->
size_t
379 return error_handler_->error_count();
382 void reset_error_count()
384 error_handler_->reset_error_count();
394 return pool_.get_statistics();
397 auto configure_threads(std::string
const& name_prefix, SchedulingPolicy policy = SchedulingPolicy::OTHER,
400 return pool_.configure_threads(name_prefix, policy, priority);
405 return pool_.set_affinity(affinity);
408 auto distribute_across_cpus() ->
bool
410 return pool_.distribute_across_cpus();
413 void wait_for_tasks()
415 pool_.wait_for_tasks();
423 [[nodiscard]]
auto size()
const noexcept ->
size_t
428 [[nodiscard]]
auto pending_tasks()
const ->
size_t
430 return pool_.pending_tasks();
435 std::shared_ptr<ErrorHandler> error_handler_;