Parallel policy states that order of iterator evaluation is not guaranteed. However, std::find*, std::search and std::mismatch all say that they return first iterator matching condition. How do those two statements interact?
As an example, let's take the following code:
std::vector<int> v(10);
std::iota(v.begin(), v.end(), 0);
const auto odd = [](int i) { return i % 2; };
const auto it = std::find(v.begin(), v.end(), odd);
assert(*it == 1);
Naive reading of documentation would allow two threads processing the chunks [0, 4] and [5, 9] in parallel and the latter returning first, resulting in *it == 5 instead. Or does it still hold that for multiple matches, the earliest is returned?
"Logically" I would say that execution policy should not alter the result, but I've used C++ long enough to not take anything for granted. I have not been able to find any relevant quote on cppreference for <execution>, <algorithm> or any of their subpages.
Not a duplicate of How does std::execution::parallel_policy interact with std::find or algorithms that modify the container? as that question is about modifying underlying container during runtime of algorithm.
According to https://eel.is/c++draft/algorithms.parallel#overloads-2
Unless otherwise specified, the semantics of calling a parallel algorithm overload are identical to calling the corresponding algorithm overload without the parameter P, using all but the first argument.
I didn't see "otherwise specification" in std::find.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With