From 923680f44bac7ca04865c5245ab5d766d6be7ffb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sun, 27 Nov 2016 19:36:45 +0100 Subject: [PATCH] abandoned my template based approach of tackling code dupe. closes #1. I've decided, that the approach was too complex, you can look at the code in commit f4eead282f758c17ffdd3c5881a230327da69078, it's some of the worst template-magic I have ever written and it didn't even work in that state. now it is handled simply via a bool parameter, the compiler should optimize it away anyway --- thread_pool.hpp | 58 ++++++++++++------------------------------------- 1 file changed, 14 insertions(+), 44 deletions(-) diff --git a/thread_pool.hpp b/thread_pool.hpp index 1648eff..914ba01 100644 --- a/thread_pool.hpp +++ b/thread_pool.hpp @@ -52,23 +52,9 @@ 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)...))>; - + template + auto _add_task_internal(bool front, F&& f, Args&&... args) + -> std::future(args)...))>; public: @@ -86,7 +72,6 @@ public: template void priority_task(std::packaged_task&& ) = delete; /* adding tasks to the queue */ - // TODO: use template-based policies to deal with priority tasks to avoid code-dupe. template auto add_task(F&& f, Args&&... args) -> std::future(args)...))>; @@ -104,17 +89,22 @@ public: /* means of getting information */ inline size_t get_thread_num() const { return workers.size(); } inline size_t get_queue_size() const { return queue.size(); } - }; -template -auto thread_pool::_add_task_internal(F&& f, Args&&... args) +template +auto thread_pool::_add_task_internal(bool front, 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); + { + std::lock_guard lock(mu); + if (front) + queue.emplace_front([pckgd_tsk](){ (*pckgd_tsk)(); }); + else + queue.emplace_back([pckgd_tsk](){ (*pckgd_tsk)(); }); + } cond.notify_one(); return pckgd_tsk->get_future(); @@ -140,34 +130,14 @@ template auto thread_pool::add_task(F&& f, Args&&... args) -> std::future(args)...))> { - auto pckgd_tsk = std::make_shared(args)...))()> > - (std::bind(std::forward(f), std::forward(args)...)); - - { - std::lock_guard lock(mu); - queue.emplace_back([pckgd_tsk](){ (*pckgd_tsk)(); }); - } - cond.notify_one(); - - return pckgd_tsk->get_future(); + return _add_task_internal(false, std::forward(f), std::forward(args)...); } template auto thread_pool::priority_task(F&& f, Args&&... args) -> std::future(args)...))> { - auto pckgd_tsk = std::make_shared(args)...))()> > - (std::bind(std::forward(f), std::forward(args)...)); - - auto ret_val= pckgd_tsk->get_future(); - - { - std::lock_guard lock(mu); - queue.emplace_front([pckgd_tsk](){ (*pckgd_tsk)(); }); - } - cond.notify_one(); - - return ret_val; + return _add_task_internal(true, std::forward(f), std::forward(args)...); } template