#include <iostream>
#include <vector>
#include <algorithm>
#include <thread>
#include <atomic>
template <typename RAIter>
int parallel_sum(RAIter begin, RAIter end)
{
std::atomic<int> result;
result = 0;
int length = end - begin;
if(length < 1000)
return std::accumulate(begin, end, 0);
std::vector<std::thread> threads;
unsigned num_of_threads = std::thread::hardware_concurrency();
threads.reserve(num_of_threads);
int values_per_thread = length / num_of_threads;
for(RAIter i = begin; i < end; i += values_per_thread) {
RAIter j = i + values_per_thread;
if(j > end) {
j = end;
}
threads.push_back(std::thread([&result](RAIter begin, RAIter end){
result += std::accumulate(begin, end, 0);
}, i, j));
}
for(auto &thread : threads)
thread.join();
return result;
}
int main()
{
std::vector<int> v(12002, 1);
int sum = parallel_sum(v.begin(), v.end());
std::cout << "The sum is " << sum << '\n';
}
Forward declarations конкретно чего?
Пункт спорный даже не из-за перекомпиляции, а из-за 1) циклической зависимости между модулями
Пространств имён должно быть намного меньше, чем файлов, плюс они должны быть предельно короткими. Избегать using namespace.
@from engine::math import Vector3D
class Object
{
// ...
private:
Vector3D position;
Vector3D speed;
}
#include <engine/math/math.i>
class Object
{
// ...
private:
engine::math::Vector3D position;
engine::math::Vector3D speed;
}