Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename ForwardRange, typename Function>
- void ForEach(ForwardRange& range, Function function) {
- size_t count_tread = thread::hardware_concurrency();
- size_t number_of_tasks = static_cast<size_t>(ceil(range.size() / count_tread));
- // количество элементов для одного потока
- size_t blocks_for_threads = number_of_tasks == 0 ? number_of_tasks + 1 : number_of_tasks;
- //cout << "blocks_for_threads = " << blocks_for_threads << endl;
- vector<ForwardRange::iterator> end_points;
- end_points.reserve(count_tread);
- size_t count = 1;
- for (auto it = range.begin(); it != range.end(); ++it) {
- if (count % blocks_for_threads == 0) {
- end_points.push_back(it);
- //cout << *it << endl;
- }
- ++count;
- }
- auto begin_point = range.begin();
- for (auto end_point = end_points.begin(); end_point != end_points.end(); ++end_point) {
- future<void> work_thread = async([begin_point, end_point, &function] {
- for_each(execution::seq, begin_point, *end_point, function);
- });
- //cout << **end_point << endl;
- begin_point = *end_point;
- }
- for_each(execution::par, begin_point, range.end(), function);
- //for_each(execution::par, range.begin(), range.end(), function);
- }
Advertisement
Add Comment
Please, Sign In to add comment