Darwin Neuroevolution Framework
Classes | Functions
pp Namespace Reference

Parallel Processing primitives. More...

Classes

class  Controller
 An optional thread pool controller, which can be used to pause/resume/cancel the queued work items. More...
 
class  ThreadPool
 A basic thread pool (managing a fixed number of threads) More...
 
struct  WorkBatch
 A collection of work items to be processed in a fork/join fashion. More...
 
class  WorkItem
 Generic work item interface. More...
 

Functions

template<class T , class Body >
void for_each (T &array, const Body &loop_body)
 Iterates over an array, with support for parallel execution. More...
 

Detailed Description

Parallel Processing primitives.

Function Documentation

◆ for_each()

template<class T , class Body >
void pp::for_each ( T &  array,
const Body &  loop_body 
)

Iterates over an array, with support for parallel execution.

pp::for_each() offers an easy way to parallelize the processing of the elements in an array (the array must support size() and operator[]):

vector<int> array;
// parallel-for-loop
pp::for_each(array, [](int index, int& value) {
... read and/or mutate the value ...
});
Note
pp::for_each() loops can't be nested
Warning
Iterations will likely happen on different threads, so the access to any shared state must be properly synchronized:
int counter = 0;
pp::for_each(array, [&](int index, int& value) {
...
// BUG: counter is captured by reference and shared between multiple
// iterations, so the increment below introduces a data race
++counter;
});