C++ std::future<bool> 在递归函数中
C++ std::future<bool> in a recursive function
考虑到以下情况,是否可以递归到函数中?我不认为 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(...)
}
...
...
...
}
澄清一下,我不确定我是否可以在不移动它的情况下传递未来?即让未来有效地检查每个递归?
使用 shared_future
,而不是(唯一的)future
。您可以通过 share()
ing 您开始的 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);
}
...
...
...
}
考虑到以下情况,是否可以递归到函数中?我不认为 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(...)
}
...
...
...
}
澄清一下,我不确定我是否可以在不移动它的情况下传递未来?即让未来有效地检查每个递归?
使用 shared_future
,而不是(唯一的)future
。您可以通过 share()
ing 您开始的 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);
}
...
...
...
}