Skip to content

Instantly share code, notes, and snippets.

@shotamatsuda
Created July 4, 2016 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shotamatsuda/4b15ea3dc616db374c38f410a934309f to your computer and use it in GitHub Desktop.
Save shotamatsuda/4b15ea3dc616db374c38f410a934309f to your computer and use it in GitHub Desktop.
Concurrent partition
// Given
const auto size = ...;
auto begin = ...;
std::list<std::future<void>> futures;
const auto concurrency = std::thread::hardware_concurrency();
const auto partition = std::imaxdiv(size, concurrency);
for (int i{}; i < concurrency; ++i) {
const auto end = begin + partition.quot + (i < partition.rem);
futures.emplace_back(std::async([this, begin, end]() {
for (auto i = begin; i != end; ++i) {
// Do something with i
}
}));
begin = end;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment