60 -> std::expected<std::unique_ptr<ThreadPool>,
rtp::Error>
62 constexpr std::string_view fmt{
"ThreadPool init failed: {}"};
65 return std::unexpected{
67 fmt,
"number of threads must be at least 1")};
70 std::unique_ptr<ThreadPool> pool{
new ThreadPool{}};
72 pool->
start(numThreads);
73 log::info(
"ThreadPool initialized successfully with {} workers.",
78 }
catch (
const std::system_error &e) {
79 return std::unexpected{
84 fmt,
"unknown error")};
91 std::unique_lock<std::mutex> lock(this->
_queueMutex);
95 "ThreadPool destroyed with {} pending tasks discarded!",
107 for (
size_t i = 0; i < numThreads; ++i)
108 this->
_workers.emplace_back([
this] (std::stop_token st)
110 this->workerThread(st);
117 std::move_only_function<void(
void)> task;
119 std::unique_lock<std::mutex> lock(this->_queueMutex);
121 this->_condition.wait(lock, [
this, st = stopToken](
void) {
122 return this->_stop || st.stop_requested()
123 || !this->_tasks.empty();
125 if ((stopToken.stop_requested() || this->_stop) &&
126 this->_tasks.empty()) [[unlikely]]
128 if (this->_tasks.empty()) [[unlikely]]
131 "Worker: Try to pop from empty queue (Logic Error)");
133 task = std::move(this->_tasks.front());
137 RTP_ASSERT(task,
"Worker: Popped an invalid/null task function!");
141 }
catch (
const std::exception &e) {
142 log::error(
"Exception in worker thread: {}", e.what());
144 log::error(
"Unknown exception in worker thread");
Assertion and verification macros for runtime checks.
#define RTP_VERIFY(condition, msg,...)
Verification macro (Debug mode)
#define RTP_ASSERT(condition, msg,...)
Assertion macro (Debug mode)
Logger declaration with support for multiple log levels.
ThreadPool class declaration.
Comprehensive error object with severity and retry tracking.
static auto failure(ErrorCode code, std::format_string< Args... > fmt, Args &&...args) -> Error
Create a failure-level error.
ThreadPool class for managing a pool of threads.
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.
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.
std::queue< std::move_only_function< void(void)> > _tasks
Queue of tasks to be executed.
void error(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an error message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
@ InvalidParameter
Invalid function parameter provided.
@ InternalRuntimeError
Internal runtime error.
@ Unknown
Unknown or unspecified error.