I would like to write a function anyElementSatisfiesPredicate
that takes in input a predicate function p
(that takes in input an object of a given type T and returns a bool) and a std::vector v
of objects of type T, and returns true if and only it exists an element in v s.t. p(v) == true.
This can be easily accomplished by means of a for loop:
bool anyElementSatisfiesPredicate(std::function<bool(T&)> p, std::vector<T> v) {
for (auto it=v.begin(); it!=v.end(); ++it)
if (p(*it))
return true;
return false;
}
This works fine (given a properly defined type T) but I would like to parallelize this code testing the predicate function p
on different elements of the vector v
at the same time. My idea is to split the work among a fixed (machine dependent) number of cores. Each thread should evaluate the predicate on a different portion of the original vector and return true as soon as it finds that the predicate p holds on an element in its portion. As soon as any given thread returns true, the core function anyElementSatisfiesPredicate
should kill the remaining threads and return true, if all threads eventually return false, it should return false instead.
Since this code will be ran on different machines with a different number of cores I would prefer not to introduce any constant defining the number of cores to be used, I'd rather have the system choose the best value for me.
Efficiency is my first concern. Is there any easy way to accomplish this (maybe using the boost threads library)?