- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
假设我想在我的 C++ 代码中使用 std::async
实现并行性,以运行计算量大的函数 func
。现在,由于它是一个繁重的功能,我们可能首先对其使用 std::launch::deferred
策略,因为情况是我们可能根本不需要运行。
但如果将来我们需要突然执行它们,我们希望并行运行。那么之后我们如何修改std::launch
策略。
[嗯,可以说你为什么不突然创建std::async
,因为突然你需要执行。但我在这里假设我不能那样做。]
或者,除了使用 std::async
之外,还有更好更简洁的方法吗?
非常感谢任何帮助。提前致谢。
#include <future>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <iostream>
std::vector<double> func(size_t n) // a computationally heavy function
{
std::vector<double> vec(n);
std::generate_n(vec.begin(), n, std::rand);
return vec;
}
int main()
{
// create asyncs, now deferred for lazy execution
auto v1 = std::async(std::launch::deferred, func, 200); // deferred for lazy execution
auto v2 = std::async(std::launch::deferred, func, 250); // deferred for lazy execution
// only after sometime we decide to execute both of them
// but we also now want them to execute in parallel
// so how can we now change the launch policy?
// to get the values as quickly as can be
auto n1 = v1.get().size();
auto n2 = v2.get().size();
std::cout<<"Got "<<n1<<" and "<<n2<<" random numbers in parallel!"<<std::endl;
return 0;
}
多想一点导致我遇到这个问题:
在用 std::launch::deferred
定义了 std::async
之后,当调用 .get()
函数时,那么保证运行async(即并行)吗?肯定不会。 http://en.cppreference.com/w/cpp/thread/launch说
it is executed on the calling thread.
然后 async 的概念就被破坏了,对吧?
最佳答案
如果 std::async
使用 std::launch::deferred
那么它会在返回的 std::future
对象的时候运行get()
函数被调用。
这表明您可以像这样强制 std::launch::async
:
int s1 = 0;
int s2 = 0;
auto v1 = std::async(std::launch::deferred, []{ return 1; });
auto v2 = std::async(std::launch::deferred, []{ return 2; });
// some fancy coding ...
if(need_to_upgrade_launch_policy())
{
auto v1a = std::async(std::launch::async, [&]{ return v1.get(); });
auto v2a = std::async(std::launch::async, [&]{ return v2.get(); });
s1 = v1a.get();
s2 = v2a.get();
}
// more clever coding ...
if(v1.valid()) // was never upgraded
s1 = v1.get();
if(v2.valid()) // was never upgraded
s2 = v2.get();
关于c++ - 之后如何为 std::async 修改 std::launch 策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574372/
运行真机调试时,出现如下问题。 Could not launch “MY-APP-NAME” iPhone has denied the launch request. Internal launch
每次单击“运行方式”按钮时,都会收到以下消息。 “选品无法推出,近期也没有推出。” 如果我选择“运行方式”按钮附近的下拉箭头,然后选择“新配置”选项(这是我设置为使用本地主机启动的配置)......它
启动过去有效的python文件时出现上述错误。我想我知道这是怎么发生的,但不确定如何解决。我有两台计算机双启动不同的操作系统,并使用保管箱保持它们之间的同步。过去,我以这种方式将我的Eclipse工作
我尝试使用 Eclipse 构建并运行“Hello world”,但它无法运行。我不使用 Android 或其他任何纯 C++。 http://i57.tinypic.com/x51h5h.png 最
这个问题在这里已经有了答案: "Selection cannot be launched and there are no recent launches” when Eclipse for Andr
在 Kotlin 中有多种启动协程的方法。我发现了几个示例,其中 GlobalScope 和 CoroutineScope 被使用。但是后者是在启动协程时直接创建的: 使用 GlobalScope :
我正在使用 10gen 版本在 OS X 上安装 MongoDB。 但是their installation tutorial事实证明对我来说有点稀缺。 到目前为止,我已经找到了安装为 launch
我在互联网上到处寻找,并尝试了论坛所说的一切,但没有任何效果。这个错误不断出现。我试过运行我的java项目(不适用于android),即使下拉运行按钮不起作用,因为它说“不适用”。 最佳答案 Ecli
我理解 std::async 对以下参数的作用。 std::launch::async std::launch::deferred 然而,std::launch::async | 会发生什么? std
我正在尝试使用 Eclipse 打开一个解压缩的 Android 项目,但每次我尝试运行或调试时都会显示此错误:“无法启动选择并且最近没有启动”。 所以我查找并尝试从现有源创建一个新项目,但打开时出现
我删除了 React Native 创建的默认启动屏幕文件,而是创建了一个启动图像。当我在模拟器中运行应用程序时,会显示启动图像,但是当我存档项目并通过 Testflight 打开它时,它仍然显示旧的
从深层链接(具有应用特定方案的 URL)启动我的 iOS 应用时,我在日志中收到此错误: lsd[738] : LaunchServices: application launch failed -
当我使用 visual studio code 调试 nodejs 应用程序时。visual studio code 告诉我 request 'launch': cannot launch targe
我创建了 Cocoa 应用程序,它是启动代理的类型。为了分发这个,我还创建了一个包,它安装该应用程序并将 launchagnet plist 文件复制到/Libraries/LaunchAgents
大家好,我今天在开发的 Java Swing 应用程序中发现了一个很大的疑问。 我发现的问题是launch() 当我在本地尝试我开发的应用程序时,它运行时没有任何延迟,但是当我使用 jnlp 从我的
我正在尝试使用 JavaFX 设置视频在 JFrame 中播放。 但是一旦我调用 launch() 函数来设置 JavaFX 并播放视频,我就会收到以下错误 应用程序构造函数中出现异常线程“AWT-E
我试图在 Android Studio 的外部工具中设置 javah 命令,但我得到了 IllegalArgumentException: Not a valid class name: Files\
我正在尝试创建一个简单的应用程序。我创建了一个新的 AVD。我启动了它。等了一个小时,模拟器只显示ANDROID。我卡住了。有人可以帮忙吗。 一个小时的样子请不要标记重复。我尽可能多地搜索和尝试。 我
进程文件: launch or launch.exe 进程名称: Vantarakis Launchh 进程类别:存在安全风险的进程 英文描述: launch.exe is a part o
将 google chrome 更新到最新版本 89.0.4389.82 (Official Build) (64-bit) 后开始出现错误。操作系统:Windows 10 错误: Puppeteer
我是一名优秀的程序员,十分优秀!