c++11 - C++ 标准::未来在递归函数中
问题描述
考虑到以下情况,是否可以递归到函数中?我不认为 std::move 会起作用,因为展开变量会消失,你会如何处理这样的事情?
auto TestFunc(std::future<bool> promisedExit
, int max
, const char* pszTesting)
{
if (promisedExit.get()) { // The calling thread will set this to cause this thread to exit.
return false;
}
...
...
...
if (max != 10) {
TestFunc(std::move(promisedExit), max++, pszTesting); // Issue is here with std::move(...)
}
...
...
...
}
澄清一下,我不确定我是否可以在不移动的情况下传递未来?即让未来有效地检查每个递归?
解决方案
使用 a shared_future
,而不是 a (unique) future
。你可以从你开始share()
的开始得到第一个future
。
auto TestFunc(std::shared_future<bool> promisedExit
, int max
, const char* pszTesting)
{
if (promisedExit.get()) { // The calling thread will set this to cause this thread to exit.
return false;
}
...
...
...
if (max != 10) {
TestFunc(promisedExit, max++, pszTesting);
}
...
...
...
}
推荐阅读
- r - R:具有多个标题的列表 - 如何按标题拆分(每个标题的行数不等)
- stripe-payments - 使用 on_behalf_of 将 Stripe 费用转入关联账户
- python - 使用多个 worker 和 Redis 部署 Flask SocketIO
- javascript - 为什么 log4js 不断拆分日志文件?
- python - 为 DataFrame 的每个部分/切片计算单独的平均值
- c - 带有 beaglebone 黑色无线的 Adafruit 终极 gps 分线模块 - UART 问题
- r - 替换 FASTA 文件中的核苷酸
- android - 在android应用程序中加载音频文件
- node.js - Angular 中的动态表仅将更改的数据发送到 API
- javascript - OAuth2 弹出窗口