diff --git a/thread_pool.hpp b/thread_pool.hpp index 1880a08..1648eff 100644 --- a/thread_pool.hpp +++ b/thread_pool.hpp @@ -52,6 +52,24 @@ class thread_pool /* the main loop of the threads */ void loop(); + class place_front_policy + { + public: + template + static void place(std::shared_ptr()(std::declval()...))>> task, + std::deque>& queue, std::mutex& mu) + { + { + std::lock_guard lock(mu); + queue.emplace_back([task](){ (*task)(); }); + } + } + }; + + template + auto _add_task_internal(F&& f, Args&&... args) -> std::future(args)...))>; + + public: /* construction & destruction */ @@ -89,6 +107,19 @@ public: }; +template +auto thread_pool::_add_task_internal(F&& f, Args&&... args) +-> std::future(args)...))> +{ + auto pckgd_tsk = std::make_shared(args)...))()> > + (std::bind(std::forward(f), std::forward(args)...)); + + placement_policy::place(pckgd_tsk, queue, mu); + cond.notify_one(); + + return pckgd_tsk->get_future(); +} + thread_pool::thread_pool(size_t thcount): fin(false) {