我们可以动态计算并创建合理的线程数目, 代码示例如下
#include <algorithm>
#include <functional>
#include <iostream>
#include <numeric>
#include <thread>
#include <vector>
template<typename Iterator, typename T>
struct accumulate_block
{
void operator()(Iterator first, Iterator last, T& result)
{
//作用累加求和,对于字符串可以将其连接起来(string类型的加,相当于字符串连接)
result = std::accumulate(first, last, result);
std::cout << "west virginia \n";
}
};
template<typename Iterator, typename T>
T parellel_accumulates(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 = std::thread::hardware_concurrency();
unsigned long const num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads);
unsigned long const block_size = length / num_threads;
std::vector<T> results(num_threads);
std::vector<std::thread> threads(num_threads - 1);
Iterator block_start = first;
for (unsigned long i = 0; i < num_threads - 1; i++)
{
Iterator block_end = block_start;
std::advance(block_end, block_size);
threads[i] = std::thread(accumulate_block<Iterator, T>(), block_start, block_end, std::ref(results[i]));
block_start = block_end;
}
accumulate_block<Iterator, T>()(block_start, last, results[num_threads - 1]);
std::for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join));
return std::accumulate(results.begin(), results.end(), init);
}
void func()
{
std::cout << "aha?\n";
}
int main()
{
func();
int n[16];
int res = parellel_accumulates(&n[0], &n[15], 16);
std::cout << res << std::endl;
int a;
std::cin >> a;
}
在我的四核机器上,显示结果如下: