Created
July 4, 2016 14:03
-
-
Save shotamatsuda/4b15ea3dc616db374c38f410a934309f to your computer and use it in GitHub Desktop.
Concurrent partition
This file contains 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
// 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