#include #include #include #include using It = std::vector::iterator; void task(It begin, It end, int& total) { int result { 0 }; for (It it { begin }; it != end; ++it) result += *it; total += result; } int sum(std::size_t thread_count, std::vector& values) { std::size_t const size { values.size() / thread_count }; std::vector pool { }; int result { }; for (std::size_t i { 0 }; i < thread_count; ++i) { auto begin { values.begin() + size * i }; auto end { values.begin() + size * (i + 1) }; pool.emplace_back(task, begin, end, std::ref(result)); } for (std::thread& thread : pool) thread.join(); return result; } int main() { std::vector v(1000000, 1); int answer { sum(20000, v) }; std::cout << answer << std::endl; }