32 namespace prediction {
42 template <
typename InputIter,
typename F>
43 void ForEach(InputIter begin, InputIter end, F f) {
44 std::vector<std::future<void>> futures;
45 for (
auto iter = begin; iter != end; ++iter) {
47 futures.emplace_back(this->
Post([&] { f(elem); }));
49 for (
auto& future : futures) {
53 AERROR <<
"Future is invalid.";
58 template <
typename FuncType>
59 std::future<typename std::result_of<FuncType()>::type>
Post(FuncType&&
func) {
60 typedef typename std::result_of<FuncType()>::type ReturnType;
61 typedef typename std::packaged_task<ReturnType()> TaskType;
65 std::shared_ptr<TaskType> task =
66 std::make_shared<TaskType>(std::move(
func));
67 std::future<ReturnType> returned_future = task->get_future();
71 return std::future<ReturnType>();
73 task_queue_.
Enqueue([task]() { (*task)(); });
74 return returned_future;
80 std::vector<std::thread> workers_;
82 std::atomic_bool stopped_;
95 ADEBUG <<
"Level = " << LEVEL
106 template <
typename InputIter,
typename F>
107 static void ForEach(InputIter begin, InputIter end, F f) {
BaseThreadPool(int thread_num, int next_thread_pool_level)
Definition: prediction_thread_pool.h:86
void(* func)(void *)
Definition: routine_context.h:41
static thread_local int s_thread_pool_level
Definition: prediction_thread_pool.h:104
std::future< typename std::result_of< FuncType()>::type > Post(FuncType &&func)
Definition: prediction_thread_pool.h:59
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
static void ForEach(InputIter begin, InputIter end, F f)
Definition: prediction_thread_pool.h:107
Definition: prediction_thread_pool.h:34
bool Enqueue(const T &element)
Definition: bounded_queue.h:110
#define ADEBUG
Definition: log.h:41
Definition: prediction_thread_pool.h:100
Definition: bounded_queue.h:37
void ForEach(InputIter begin, InputIter end, F f)
Definition: prediction_thread_pool.h:43
static std::vector< int > THREAD_POOL_CAPACITY
Definition: prediction_thread_pool.h:77
static LevelThreadPool * Instance()
Definition: prediction_thread_pool.h:88
#define AERROR
Definition: log.h:44