diff --git a/thread_pool/thread_pool.hpp b/thread_pool/thread_pool.hpp index ee3ea034..13264509 100644 --- a/thread_pool/thread_pool.hpp +++ b/thread_pool/thread_pool.hpp @@ -13,9 +13,11 @@ * @brief The ThreadPoolOptions struct provides construction options for ThreadPool. */ struct ThreadPoolOptions { - enum {AUTODETECT = 0}; - size_t threads_count = AUTODETECT; - size_t worker_queue_size = 1024; + ThreadPoolOptions() = default; + ThreadPoolOptions(size_t queue_size, size_t count = 0) : threads_count(count), worker_queue_size(queue_size) {} + enum { AUTODETECT = 0 }; + size_t threads_count = AUTODETECT; + size_t worker_queue_size = 1024; }; /** @@ -46,7 +48,7 @@ class ThreadPool { * execution or exception thrown. */ template - void post(Handler &&handler); + bool post(Handler &&handler); /** * @brief process Post piece of job to thread pool and get future for this job. @@ -104,11 +106,9 @@ inline ThreadPool::~ThreadPool() } template -inline void ThreadPool::post(Handler &&handler) +inline bool ThreadPool::post(Handler &&handler) { - if (!getWorker().post(std::forward(handler))) { - throw std::overflow_error("worker queue is full"); - } + return getWorker().post(std::forward(handler)); } template @@ -120,8 +120,8 @@ typename std::future ThreadPool::process(Handler &&handler) std::future result = task.get_future(); - if (!getWorker().post(task)) { - throw std::overflow_error("worker queue is full"); + if (!post(task)) { + return nullptr; } return result; diff --git a/thread_pool/worker.hpp b/thread_pool/worker.hpp index 3d51da5c..ca8bb19c 100644 --- a/thread_pool/worker.hpp +++ b/thread_pool/worker.hpp @@ -78,7 +78,7 @@ class Worker { namespace detail { inline size_t * thread_id() { - static thread_local size_t tss_id = -1u; + static thread_local size_t tss_id = std::numeric_limits::max(); return &tss_id; } }