started working on template-policy based implementation of add_task().

This commit is contained in:
Kjistóf 2016-11-27 18:27:06 +01:00
parent 4cd6eb4e2f
commit f4eead282f

View File

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