Я пытаюсь распараллелить код, который суммирует плюс один для всех элементов вектора M, с помощью ThreadPool (пул стандартен и реализован в книге Parallel Концепции программирования и практика). Поэтому я определил функцию sum_plus_one(x,M), которая принимает в качестве аргументов x: положение вектора и вектор M. Код следующий:
Код: Выделить всё
#include
#include
#include
#include
#include
int main(int argc, char *argv[]) {
// define a Thread Pool with 8 Workers
ThreadPool TP(5);
auto sum_plus_one = [](const uint64_t x, const std::vector &M) {
return M[x]+1;
};
uint64_t N = 10; // dimension of the vector
// allocate the vector
std::vector M(N);
std::vector futures;
// init function
auto init=[&]() {
for(uint64_t i = 0; i< N; ++i) {
M[i] = 1;
}
};
init();
// enqueue the tasks in a linear fashion
for (uint64_t x = 0; x < N; ++x) {
futures.emplace_back(TP.enqueue(sum_plus_one, x, &M));
}
// wait for the results
for (auto& future : futures)
std::cout std::future {
Подробнее здесь: https://stackoverflow.com/questions/782 ... mplace-bac