如何使用新的C ++ 17执行策略? [英] How do I use the new C++17 execution policies?

查看:91
本文介绍了如何使用新的C ++ 17执行策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 std :: algorithm cppreference.com 中的文档,我注意到了一个C ++ 17标记在很多我还没有使用过的很酷的东西上。最引起我注意的是新的执行策略。通过阅读有关它们的信息,我发现,只要指定执行策略,就可以使任何我想要多线程的 for_each 循环。

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.

例如,我有一个程序可以输出带有2D图形的图像。

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;
}

但是,当我第一次尝试此操作时(使用 g ++- std = c ++ 17 )我收到一条错误消息,告诉我尚未声明'std :: execution',所以我尝试添加 #include< execution> ,但显示 execution:没有这样的文件或目录。我也试过#include< experimental / algorithm> 而不是#include< algorithm> ,但我得到的却是相同的结果。我如何使用这项新功能?

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?

推荐答案

尚未完成。并且各种编译器尚未完全实现它。

c++17 was not yet finalized. And various compilers have not yet fully implemented it.

-std = c ++ 17 的意思是全部给我已完成的C ++ 17版本,而不是成为完全有效的C ++ 17编译器。

-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.

没有公认的如果您完全支持C ++ 17,请给我,否则给我一个C ++ 17。错误标志,您可以将其传递给编译器;部分是因为它几乎没有实际用途。如果他们提供的C ++ 17子集足够,您就赢了。而且,如果您需要完全兼容的编译器,则特定版本的编译器将不知道它们是否存在错误,因此您无论如何都不能相信该标志,而必须针对编译器版本对其进行测试。而且,如果您已经知道哪个版本的编译器具有足够有效的C ++ 17,则不需要标记即可告诉您。

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.

这篇关于如何使用新的C ++ 17执行策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆