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

17,761

Solution 1

was not yet finalized. And various compilers have not yet fully implemented 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.

There is no generally accepted "please give me C++17 if you fully support it, and otherwise give me an error" flag you can pass to a compiler; partly because it is of little practical use. If the subset of C++17 they provide is sufficient, then you win. And if you need a fully compliant compiler, specific versions of compilers don't know if they have bugs, so you couldn't trust the flag anyhow and would have to test it against compiler versions. And if you already know what versions of the compiler have sufficiently valid C++17, you don't need a flag to tell you.

Solution 2

As far as I understand from cppreference this feature is defined in document P0024R2 and it is not yet supported in any compiler.

Solution 3

If you're using g++ then you can try the non-standard extension:

https://gcc.gnu.org/onlinedocs/libstdc++/manual/parallel_mode.html

Solution 4

For Microsoft compiler: see C++17 Progress in VS 2017 15.5 and 15.6 where you will find:

Status  Std   Paper   Title
Partial C++17 P0024R2 Parallel Algorithms

For GCC, as Fanael wrote in his comment, see Table 1.5. C++ 2017 Implementation Status at https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2017 where you will find

Library Feature                             Proposal    Status
The Parallelism TS Should be Standardized   P0024R2     No
Share:
17,761
Willy Goat
Author by

Willy Goat

Dumb.

Updated on July 02, 2022

Comments

  • Willy Goat
    Willy Goat almost 2 years

    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?