gpt4 book ai didi

c++ - 在 VC++ PPL 中,如何创建同步返回的任务返回方法?

转载 作者:太空狗 更新时间:2023-10-29 19:53:18 25 4
gpt4 key购买 nike

考虑以下 C# 代码:

async Task DoSomethingAsync()
{
if (m_f)
return;
await DoSomethingInternalAsync();
}

编译器将其转换为任务返回调用,如果 m_f 为真,则任务立即完成,否则,它将异步操作“委托(delegate)”给 DoSomethingInternalAsync()。

现在,我如何在 C++ 中执行此操作?代码应如下所示:

task<void> DoSomethingAsync()
{
if (m_f)
return [[What do I return here so the task is complete (.then called immediately)?!]];
return DoSomethingInternalAsync();
}

Edit1:在 C# 中,我可以使用 TaskCompletionSource<> 来做同样的事情,但不使用 async 关键字 - 本质上是创建一个已完成的任务。

最佳答案

另一种方法是 task_from_result .您可以使用 concurrency::task_from_result()对于 task<void>方法和 concurrency::task_from_result(theReturnValue)对于 task<T>方法。我相信这是 Visual Studio 2013 中的新起点。

关于c++ - 在 VC++ PPL 中,如何创建同步返回的任务返回方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15492953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com