Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::adapters::thread_pool_executor_adapter Class Reference

Adapter exposing thread_pool through common::interfaces::IExecutor. More...

#include <common_executor_adapter.h>

Inheritance diagram for kcenon::thread::adapters::thread_pool_executor_adapter:
Inheritance graph
Collaboration diagram for kcenon::thread::adapters::thread_pool_executor_adapter:
Collaboration graph

Public Member Functions

 thread_pool_executor_adapter (std::shared_ptr< kcenon::thread::thread_pool > pool)
 
std::future< void > submit (std::function< void()> task)
 
std::future< void > submit_delayed (std::function< void()> task, std::chrono::milliseconds delay)
 
common::Result< std::future< void > > execute (std::unique_ptr< common::interfaces::IJob > &&job) override
 
common::Result< std::future< void > > execute_delayed (std::unique_ptr< common::interfaces::IJob > &&job, std::chrono::milliseconds delay) override
 
size_t worker_count () const override
 
bool is_running () const override
 
size_t pending_tasks () const override
 
void shutdown (bool wait_for_completion=true) override
 
std::shared_ptr< kcenon::thread::thread_poolget_thread_pool () const
 

Private Attributes

std::shared_ptr< kcenon::thread::thread_poolpool_
 

Detailed Description

Adapter exposing thread_pool through common::interfaces::IExecutor.

This is the recommended way to use thread_pool with the IExecutor interface. It provides a clean separation between thread_pool's core functionality and the IExecutor interface contract.

Example Usage
// Create and configure the thread pool
auto pool = std::make_shared<kcenon::thread::thread_pool>("my_pool");
pool->enqueue(std::make_unique<kcenon::thread::thread_worker>());
pool->start();
// Create the adapter for IExecutor compatibility
auto executor = std::make_shared<kcenon::thread::adapters::thread_pool_executor_adapter>(pool);
// Use through IExecutor interface
auto future = executor->execute(std::make_unique<MyJob>());
Adapter to bridge thread_system pools with common IExecutor interface.
Core thread pool implementation with work stealing and auto-scaling.
Note
The adapter holds a shared_ptr to the thread_pool, ensuring the pool remains alive as long as the adapter exists.

Definition at line 282 of file common_executor_adapter.h.

Constructor & Destructor Documentation

◆ thread_pool_executor_adapter()

kcenon::thread::adapters::thread_pool_executor_adapter::thread_pool_executor_adapter ( std::shared_ptr< kcenon::thread::thread_pool > pool)
inlineexplicit

Definition at line 284 of file common_executor_adapter.h.

285 : pool_(std::move(pool)) {}
std::shared_ptr< kcenon::thread::thread_pool > pool_

Member Function Documentation

◆ execute()

common::Result< std::future< void > > kcenon::thread::adapters::thread_pool_executor_adapter::execute ( std::unique_ptr< common::interfaces::IJob > && job)
inlineoverride

Definition at line 315 of file common_executor_adapter.h.

315 {
316 auto shared_job = std::shared_ptr<common::interfaces::IJob>(std::move(job));
318 [shared_job]() mutable -> common::VoidResult {
319 try {
320 auto result = shared_job->execute();
321 if (result.is_err()) {
322 return detail::make_error(result.error());
323 }
324 return common::ok();
325 } catch (const std::exception& ex) {
326 return detail::make_error(
328 ex.what());
329 } catch (...) {
330 return detail::make_error(
332 "Unknown exception while executing common job");
333 }
334 });
335 }
common::VoidResult make_error(const common::error_info &info)
common::Result< std::future< void > > schedule_task(const std::shared_ptr< kcenon::thread::thread_pool > &pool, std::function< common::VoidResult()> body)

References kcenon::thread::job_execution_failed, kcenon::thread::adapters::detail::make_error(), pool_, and kcenon::thread::adapters::detail::schedule_task().

Here is the call graph for this function:

◆ execute_delayed()

common::Result< std::future< void > > kcenon::thread::adapters::thread_pool_executor_adapter::execute_delayed ( std::unique_ptr< common::interfaces::IJob > && job,
std::chrono::milliseconds delay )
inlineoverride

Definition at line 337 of file common_executor_adapter.h.

339 {
340 auto promise = std::make_shared<std::promise<void>>();
341 auto future = promise->get_future();
342 auto shared_job = std::shared_ptr<common::interfaces::IJob>(std::move(job));
343
345 [shared_job]() mutable -> common::VoidResult {
346 try {
347 auto result = shared_job->execute();
348 if (result.is_err()) {
349 return detail::make_error(result.error());
350 }
351 return common::ok();
352 } catch (const std::exception& ex) {
353 return detail::make_error(
355 ex.what());
356 } catch (...) {
357 return detail::make_error(
359 "Unknown exception while executing common job");
360 }
361 },
362 delay);
363
364 return common::Result<std::future<void>>::ok(std::move(future));
365 }
@ delay
Delay processing (attempt later)
void schedule_task_async(std::shared_ptr< kcenon::thread::thread_pool > pool, std::shared_ptr< std::promise< void > > promise, std::function< common::VoidResult()> body, std::chrono::milliseconds delay)

References kcenon::thread::delay, kcenon::thread::job_execution_failed, kcenon::thread::adapters::detail::make_error(), pool_, and kcenon::thread::adapters::detail::schedule_task_async().

Here is the call graph for this function:

◆ get_thread_pool()

std::shared_ptr< kcenon::thread::thread_pool > kcenon::thread::adapters::thread_pool_executor_adapter::get_thread_pool ( ) const
inline

Definition at line 392 of file common_executor_adapter.h.

392 {
393 return pool_;
394 }

References pool_.

◆ is_running()

bool kcenon::thread::adapters::thread_pool_executor_adapter::is_running ( ) const
inlineoverride

Definition at line 371 of file common_executor_adapter.h.

371 {
372 return pool_ && pool_->is_running();
373 }
auto is_running() const -> bool
Check if the thread pool is currently running.

References kcenon::thread::thread_pool::is_running(), and pool_.

Here is the call graph for this function:

◆ pending_tasks()

size_t kcenon::thread::adapters::thread_pool_executor_adapter::pending_tasks ( ) const
inlineoverride

Definition at line 375 of file common_executor_adapter.h.

375 {
376 return pool_ ? pool_->get_pending_task_count() : 0U;
377 }
auto get_pending_task_count() const -> std::size_t
Get the number of pending tasks in the queue.

References kcenon::thread::thread_pool::get_pending_task_count(), and pool_.

Here is the call graph for this function:

◆ shutdown()

void kcenon::thread::adapters::thread_pool_executor_adapter::shutdown ( bool wait_for_completion = true)
inlineoverride

Definition at line 379 of file common_executor_adapter.h.

379 {
380 if (!pool_) {
381 return;
382 }
383
384 auto stop_result = pool_->stop(!wait_for_completion);
385 if (stop_result.is_err()) {
386 // Best effort: surface error via exception to aid debugging.
387 const auto& err = stop_result.error();
388 throw std::runtime_error(err.message);
389 }
390 }
auto stop(const bool &immediately_stop=false) -> common::VoidResult
Stops the thread pool and all worker threads.

References pool_, and kcenon::thread::thread_pool::stop().

Here is the call graph for this function:

◆ submit()

std::future< void > kcenon::thread::adapters::thread_pool_executor_adapter::submit ( std::function< void()> task)
inline

Definition at line 287 of file common_executor_adapter.h.

287 {
288 auto result = detail::schedule_task(pool_, [task = std::move(task)]() mutable {
289 return detail::wrap_user_task(task);
290 });
291
292 if (result.is_ok()) {
293 return std::move(result.unwrap());
294 }
295
296 std::promise<void> failed;
297 failed.set_exception(detail::to_exception(result.error()));
298 return failed.get_future();
299 }
common::VoidResult wrap_user_task(const std::function< void()> &task)
std::exception_ptr to_exception(const common::error_info &info)
@ failed
Execution failed.

References kcenon::thread::failed, kcenon::thread::result< T >::is_ok(), pool_, kcenon::thread::adapters::detail::schedule_task(), kcenon::thread::adapters::detail::to_exception(), and kcenon::thread::adapters::detail::wrap_user_task().

Here is the call graph for this function:

◆ submit_delayed()

std::future< void > kcenon::thread::adapters::thread_pool_executor_adapter::submit_delayed ( std::function< void()> task,
std::chrono::milliseconds delay )
inline

Definition at line 301 of file common_executor_adapter.h.

302 {
303 auto promise = std::make_shared<std::promise<void>>();
304 auto future = promise->get_future();
305
307 [task = std::move(task)]() mutable {
308 return detail::wrap_user_task(task);
309 },
310 delay);
311
312 return future;
313 }

References kcenon::thread::delay, pool_, kcenon::thread::adapters::detail::schedule_task_async(), and kcenon::thread::adapters::detail::wrap_user_task().

Here is the call graph for this function:

◆ worker_count()

size_t kcenon::thread::adapters::thread_pool_executor_adapter::worker_count ( ) const
inlineoverride

Definition at line 367 of file common_executor_adapter.h.

367 {
368 return pool_ ? pool_->get_active_worker_count() : 0U;
369 }
auto get_active_worker_count() const -> std::size_t
Get the current number of active (running) workers.

References kcenon::thread::thread_pool::get_active_worker_count(), and pool_.

Here is the call graph for this function:

Member Data Documentation

◆ pool_

std::shared_ptr<kcenon::thread::thread_pool> kcenon::thread::adapters::thread_pool_executor_adapter::pool_
private

The documentation for this class was generated from the following file: