36#if defined(PTL_USE_TBB)
45#if defined(PTL_USE_TBB)
55template <
typename Tp,
typename Arg = Tp>
60 typedef decay_t<Arg> ArgTp;
61 typedef TBBTaskGroup<Tp, Arg> this_type;
62 typedef TaskGroup<Tp, Arg> base_type;
63 typedef typename base_type::result_type result_type;
64 typedef typename base_type::packaged_task_type packaged_task_type;
65 typedef typename base_type::future_type future_type;
66 typedef typename base_type::promise_type promise_type;
70 template <
typename... Args>
71 using task_type = Task<ArgTp, decay_t<Args>...>;
76 template <
typename Func>
78 : base_type(std::forward<Func>(_join), _tp)
81 template <typename Up = Tp, enable_if_t<std::is_same<Up, void>::value,
int> = 0>
88 virtual ~TBBTaskGroup()
90 delete m_tbb_task_group;
100 this_type& operator=(
const this_type& rhs) =
delete;
102 this_type& operator=(this_type&& rhs) =
default;
106 template <
typename Up>
107 Up* operator+=(Up* _task)
110 vtask_list.push_back(_task);
114 m_task_set.push_back(std::move(_task->get_future()));
121 template <
typename Func,
typename... Args>
122 task_type<Args...>* wrap(Func&& func, Args... args)
125 new task_type<Args...>(
this, std::forward<Func>(func), args...));
130 template <
typename Func,
typename... Args>
131 void run(Func&& func, Args... args)
133 auto _func = [=]() {
return func(args...); };
134 auto _task = wrap(std::move(_func));
135 auto _lamb = [=]() { (*_task)(); };
136 m_tbb_task_group->run(_lamb);
139 template <
typename Func,
typename... Args>
140 void exec(Func&& func, Args... args)
142 auto _func = [=]() {
return func(args...); };
143 auto _task = wrap(std::move(_func));
144 auto _lamb = [=]() { (*_task)(); };
145 m_tbb_task_group->run(_lamb);
148 template <
typename Func,
typename... Args,
typename Up = Tp,
149 enable_if_t<std::is_same<Up, void>::value,
int> = 0>
150 void parallel_for(uintmax_t nitr, uintmax_t, Func&& func, Args... args)
152 tbb::parallel_for(tbb::blocked_range<size_t>(0, nitr),
153 [&](
const tbb::blocked_range<size_t>& range) {
154 for(
size_t i = range.begin(); i != range.end(); ++i)
162 virtual bool is_native_task_group()
const override {
return false; }
166 virtual void wait()
override
169 m_tbb_task_group->wait();
173 using base_type::begin;
174 using base_type::cbegin;
175 using base_type::cend;
176 using base_type::clear;
177 using base_type::end;
178 using base_type::get_tasks;
182 template <typename Up = Tp, enable_if_t<!std::is_void<Up>::value,
int> = 0>
183 inline Up join(Up accum = {})
186 for(
auto& itr : m_task_set)
188 using RetT =
decay_t<
decltype(itr.get())>;
189 accum = m_join(std::ref(accum), std::forward<RetT>(itr.get()));
196 template <
typename Up = Tp,
typename Rp = Arg,
197 enable_if_t<std::is_void<Up>::value && std::is_void<Rp>::value,
int> = 0>
201 for(
auto& itr : m_task_set)
208 template <
typename Up = Tp,
typename Rp = Arg,
209 enable_if_t<std::is_void<Up>::value && !std::is_void<Rp>::value,
int> = 0>
213 for(
auto& itr : m_task_set)
215 using RetT =
decay_t<
decltype(itr.get())>;
216 m_join(std::forward<RetT>(itr.get()));
224 using base_type:: operator++;
225 using base_type:: operator--;
226 using base_type::m_join;
227 using base_type::m_task_set;
228 using base_type::vtask_list;
235template <
typename Tp,
typename Arg = Tp>
TaskGroup< Tp, Arg > TBBTaskGroup
typename std::decay< T >::type decay_t
tbb::task_group tbb_task_group_t
std::function< JoinT(JoinT &, JoinArg &&)> Type