我正在阅读 C++ Concurrency In Action,第 32 页(第 2 章)中有这段代码。
template <typename Iterator, typename T>
struct accumulate_block
{
void operator()(Iterator first, Iterator last, T &result)
{
result = std::accumulate(first, last, result);
}
};
template <typename Iterator, typename T>
T parallel_accumulate(Iterator first, Iterator last, T init)
{
unsigned long const length = std::distance(first, last);
if (!length)
return init;
unsigned long const min_per_thread = 25;
unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread;
unsigned long const hardware_threads = …Run Code Online (Sandbox Code Playgroud)