Willy Goat Willy Goat - 1 month ago 7
C++ Question

How do I use the new C++17 execution policies?

I was reading through the std::algorithm documentation at cppreference.com and I noticed a C++17 tag on a lot of cool things I haven't used yet. What got my attention most was the new execution policies. What I gathered from reading about them is that I can make any

for_each
loop I want multi-threaded just by specifying an execution policy.

For example, I have a program which outputs an image with a 2D graphic on it.

int main(){
std::for_each(
img.buffer().begin(),
img.buffer().end(),
renderer(
{-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
img,
16
)
);
fout << img;
}


If I want to make this program multi-threaded I should be able to do it with one line.

int main(){
std::for_each(
std::execution::par_unseq, // c++17 feature
img.buffer().begin(),
img.buffer().end(),
renderer(
{-0.5, 0.0, 2.666, 2.0, M_PI / 2.0, 0.0},
img,
16
)
);
fout << img;
}


However when I first tried this (with
g++ -std=c++17
) I got an error telling me that
‘std::execution’ has not been declared
, so I tried adding
#include <execution>
but it says
execution: No such file or directory
. I've also tried
#include<experimental/algorithm>
instead of
#include<algorithm>
but I get the same result. How do I use this new feature?

Answer Source

C++17 has not been finalized. And various compilers have not yet fully implemeneted it.

-std=c++17 means "give me all of C++17 you have finished", not "be a perfectly valid C++17 compiler".

This feature is not supported by your compiler and/or standard library at this point. Check back in a few weeks/months/years.