| 44 | |
| 45 | template <typename ITER_FUNC_T> |
| 46 | inline void ForEach(int from, int to, const ITER_FUNC_T& iter_func) { |
| 47 | std::atomic<int> cur(from); |
| 48 | |
| 49 | std::vector<std::future<void>> results(thread_num_); |
| 50 | for (uint32_t tid = 0; tid < thread_num_; ++tid) { |
| 51 | results[tid] = thread_pool_.enqueue( |
| 52 | [&cur, &iter_func, to](int tid) { |
| 53 | while (true) { |
| 54 | int got = cur.fetch_add(1); |
| 55 | if (got >= to) { |
| 56 | break; |
| 57 | } |
| 58 | iter_func(tid, got); |
| 59 | } |
| 60 | }, |
| 61 | tid); |
| 62 | } |
| 63 | |
| 64 | thread_pool_.WaitEnd(results); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @brief Iterate on vertexes of a VertexRange concurrently. |