According to the execution_policy std::mutex can't be used with std::execution::par_unseq with the following example on the page:
int x = 0;
std::mutex m;
int a[] = {1, 2};
std::for_each(std::execution::par_unseq, std::begin(a), std::end(a), [&](int)
{
std::lock_guard<std::mutex> guard(m); // Error: lock_guard constructor calls m.lock()
++x;
});
What is the specific reason or scenario which makes this illegal?
Is the main reason that with vectorization the code might try to get std::mutex::lock from the same thread and:
If lock is called by a thread that already owns the mutex, the behavior is undefined: for example, the program may deadlock.
Or there is another explanation on what exactly and why could go wrong?