如何在 C++/WinRT 中等待多个 awaitables/IAsyncActions(等效于“Promise.all”)?

How can I await multiple awaitables/IAsyncActions in C++/WinRT (`Promise.all` equivalent)?

有没有等价于JavaScript的Promise.all in C++ or C++/WinRT for awaitables (or just Windows.Foundation.IAsyncAction)?

例如,我正在尝试收集多个 IAsyncActions 并在所有 IAsyncActions 完成后继续。现在我开始每个动作,然后在一个简单的循环中一个一个地等待它们:

winrt::IAsyncAction AwaitMultiple(std::vector<winrt::IAsyncAction> actions)
{
    for (const auto& action : actions)
    {
        co_await action;
    }
}

// ... somewhere else...

winrt::IAsyncAction MyFunctionAsync()
{
    std::vector<winrt::IAsyncAction> actions{};
    actions.push_back(/* some function that generates an IAsyncAction */);
    actions.push_back(/* some function that generates an IAsyncAction */);
    actions.push_back(/* some function that generates an IAsyncAction */);
    // ... etc...

    co_await AwaitMultiple(actions);

    // ... do other stuff...
}

有更好的方法吗?有语言方式吗?

通常情况下,几乎这种情况确实有一个 C++/WinRT 函数:winrt::when_all(T... async) (it is not currently documented on Advanced concurrency and asynchrony with C++/WinRT).

如果您有现代 C++/WinRT,您可以同时等待多个操作:

winrt::IAsyncAction MyFunctionAsync()
{
    const auto myAction = /* function that generates an IAsyncAction */;
    const auto myAction2 = /* function that generates an IAsyncAction */;
    const auto myAction3 = /* function that generates an IAsyncAction */;

    // magic!
    co_await winrt::when_all(myAction, myAction2, myAction3);
}

这类似于 the Parallel Pattern Library (PPL) when_all

这并不能解决上面的 std::vector<winrt::IAsyncAction>,但幸运的是 Raymond Chen 有一些关于这个的帖子:

A customer wanted to know how they could pass a std::vector of IAsyncAction objects to the when_all function.

std::vector<IAsyncAction> actions = get_actions();
for (auto&& action : actions) co_await action;

[... some more content about trying to wrap that in an overload for winrt::when_all...]

从已有的部分合成 when_all 协程


另请参阅: