44#ifndef RTYPE_THREADPOOL_HPP_
45 #define RTYPE_THREADPOOL_HPP_
50 #include <condition_variable>
93 static auto create(
size_t numThreads)
94 -> std::expected<std::unique_ptr<ThreadPool>,
rtp::Error>;
121 template<
std::invocable F, typename... Args>
124 ->
std::expected<
std::future<
std::invoke_result_t<F, Args...>>,
162 void start(
size_t numThreads);
Error handling system with categorized error codes.
Comprehensive error object with severity and retry tracking.
ThreadPool class for managing a pool of threads.
ThreadPool(void) noexcept=default
Private constructor to initialize the ThreadPool.
bool _stop
Flag indicating whether the ThreadPool is stopping.
void workerThread(std::stop_token stopToken) noexcept
The worker thread function that continuously processes tasks.
std::mutex _queueMutex
Mutex for synchronizing access to the task queue.
static auto create(size_t numThreads) -> std::expected< std::unique_ptr< ThreadPool >, rtp::Error >
Creates a ThreadPool with the specified number of threads.
ThreadPool(const ThreadPool &)=delete
ThreadPool & operator=(const ThreadPool &)=delete
std::vector< std::jthread > _workers
Vector of worker threads.
void start(size_t numThreads)
Starts the worker threads in the pool.
~ThreadPool() noexcept
Destroys the ThreadPool, ensuring all threads are joined and resources are released.
std::condition_variable _condition
Condition variable for notifying worker threads.
auto enqueue(F &&f, Args &&...args) -> std::expected< std::future< std::invoke_result_t< F, Args... > >, rtp::Error >
Enqueues a task for asynchronous execution.
std::queue< std::move_only_function< void(void)> > _tasks
Queue of tasks to be executed.