Created
January 6, 2019 18:43
-
-
Save DennisOSRM/c800845e84a72ef2de9808afb4bd2609 to your computer and use it in GitHub Desktop.
macOS parallel for each
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iterator> | |
#include <utility> | |
#include "dispatch/dispatch.h" | |
template<typename It, typename F> | |
void parallel_for_each(It a, It b, F&& f) | |
{ | |
size_t count=std::distance(a,b); | |
using data_t=std::pair<It,F>; | |
data_t helper=data_t(a,std::forward<F>(f)); | |
dispatch_apply_f(count, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), &helper, [](void* ctx,size_t cnt) | |
{ | |
data_t* d=static_cast<data_t*>(ctx); | |
auto elem_it=std::next(d->first,cnt); | |
(*d).second(*(elem_it)); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment