我不知道的适应度函数返回,更不用说做,但一个想法是围绕编写一个包装的功能是什么它称它为“m”倍 - 在这种情况下为100/8或12倍。然后创建一个循环“n”次,每次调用thread_group :: add_thread,这会产生一个调用wrapper函数的新线程。
的基本想法是这样的:
/* ??? */ fitness_calculation(organism& o){
//...
}
// wrapper function
void calc(std::vector<organism>& v, int idx, int loops){
for(int i = 0; i < loops; i++)
fitness_calculation(v[idx + i]);
}
int main(){
int num_organisms = 100;
std::vector<organism> v(num_organisms); // some array with the organisms
int threads = 8;
boost::thread_group g;
int organisms_per_thread = num_organisms/threads;
int i = 0, idx = 0;
for ( ; i < threads; ++i, idx += organisms_per_thread)
g.add_thread(calc, v, idx, organisms_per_thread);
// finish up remainder in this thread
calc(v, idx, num_organisms % threads);
g.join_all();
}
我不知道如果我有thread_group函数调用语法正确的,但它的足够接近。希望这有助于。