From f4eead282f758c17ffdd3c5881a230327da69078 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 27 Nov 2016 18:27:06 +0100 Subject: [PATCH] started working on template-policy based implementation of add_task(). --- thread_pool.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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) {