renestein / Rstein.AsyncCpp

The RStein.AsyncCpp library is a set of types that should be familiar for anyone who knows the Task Parallel Library (TPL) for .NET (C#).
MIT License
31 stars 4 forks source link

Can't co_await a std::shared_future in a c++/winrt UWP app (doesn't compile) #14

Closed lawabider closed 3 years ago

lawabider commented 3 years ago

Do you have any idea why this is? I have set the c++ version to latest and added /await compiler flag.
I am able to co_await a std::future, so I am probably able to use a lot of Rstein.AsyncCpp already.

This doesn't compile:

Windows::Foundation::IAsyncAction MainPage::ClickHandler(IInspectable const&, RoutedEventArgs const&)
{
    std::promise<void> promise;
    promise.set_value();
    std::shared_future<void>  _completedFuture = promise.get_future().share();
    co_await _completedFuture;
}

I get: 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\future(1034,1): error C3312: no callable 'await_resume' function found for type 'std::shared_future' 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\future(1034,1): error C3312: no callable 'await_ready' function found for type 'std::shared_future' 1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\include\future(1034,1): error C3312: no callable 'await_suspend' function found for type 'std::shared_future'

So this isn't compiling:

  auto transform1 = DataFlowAsyncFactory::CreateTransformBlock<int, string>([](const int& item)-> Tasks::Task<string>
                                                      {
                                                        auto message = "int: " + to_string(item) + "\n";
                                                        cout << message;
                                                        //await async operation returning standard shared_future.
                                                        co_await GetCompletedSharedFuture();
                                                        co_return to_string(item);
                                                      });
renestein commented 3 years ago

Did you include FutureEx.h? #include "path/to/RStein.AsyncCpp/AsyncPrimitives/FutureEx.h"

Please be aware that support for the shared_future is experimental and should be used only for quick integration of the existing code. Due to the internal structure of the std::shared_future, an awaiter s not efficient and wastes thread. (https://github.com/renestein/Rstein.AsyncCpp/blob/master/RStein.AsyncCpp/AsyncPrimitives/FutureEx.h#L103)

Please use the type Task instead of the std::future/std::shared_future whenever is possible. https://github.com/renestein/Rstein.AsyncCpp/blob/master/RStein.AsyncCpp/Tasks/Task.h

Also please be aware that I will soon upgrade the code to the 'standard' coroutine support on Visual Studio 2019 (/await switch is obsolete). (https://devblogs.microsoft.com/cppblog/c-coroutines-in-visual-studio-2019-version-16-8/) This change will also have an impact on the shared_future (a drop of the experimental namespace for coroutine etc.)

lawabider commented 3 years ago

I did include FutureEx.h. I'll just avoid std::shared_future for now.

renestein commented 3 years ago

Closing. Feel free to reopen.