Async Programming Improvements for C++ and UWP

Sign in to queue

Description

In this video, you’ll see how C++ Coroutines can dramatically simplify the Async coding pattern for UWP, whilst boosting readability and maintainability. As with many other languages, C++ now has an “await”-like feature that lets functions suspend and resume without blocking their threads. The new C++ co_await keyword can be used with both standard C++ and C++/CX, and is supported for both Universal Windows Apps and Classic Windows Apps.

Day:

0

Session Type:

Pre-recorded

Code:

P489

Embed

Download

The Discussion

  • User profile image
    raulp

    I have 2 questions. 

    1. Can C++ events be marked as async some how so they can await async functions? For example a XAML button click event.
    2. Can a void function be marked as async without making it as a a task such as done in C# (async void Func())? This would allow non async functions to call the function synchronously while the internal function can still use co_await. Would be useful in modifying existing code without having to modify return types all the way up the call stack.
  • User profile image
    tnx

    Hi, how to handle exceptions ?

  • User profile image
    dea398

    Can I get access to these new features con Win8.1 c++/cx projects?

  • User profile image
    cmello

    Excellent congratulations for the great work!! I'm installing VS2015 Update 2 to play with it! Thank you very much!

  • User profile image
    Mat

    I think C++98 two-phase template parsing would be a win for compile-time performance. MSVC parses the entire template every time a new instantiation is called for. Clang and GCC parse the non-dependent parts of a template only once.

  • User profile image
    EricMitt

    @dea398: Yes, but only from VS2015, Update2 is better as we reinforce Coroutines in it

  • User profile image
    GorNishanov

    @RaulP:

    There are two ways of doing events:

    1. Create a task<void> EventHandlerImpl(int p) { ... co_await ... } and have a simple forwarder:

    void EventHandler(int p) { EventHandlerImpl(p); }

    2. Specialize std::experimental::coroutine_traits<void, Whatever...> to be the same as std::experimental::coroutine_traits<concurrency::tasl<void>, Whatever...> with the exception of get_return_object. This will allow you to author coroutines return void type. It can look something like:

    namespace std { namespace experimental {
      template <typename... Whatever>
      struct coroutine_traits<void, Whatever...> {
        using base = typename coroutine_traits<concurrency::task<void>, Whatever...>::promise_type;
        struct promise_type : base { void get_return_object() { base::get_return_object(); } };
      };
    }}

    Now you can simply write:

    void Handler1(int p) { ... } // normal function

    void Handler2(int p) { .... co_await ... } // coroutine

    Cheers,

    Gor

     

     

     

  • User profile image
    GorNishanov

    @tnx:

    set_exception member of coroutine promise_type controls the behavior what to do if exception escapes the coroutine.

    If set_exception is not present, exception propagates through the call stack normally. (std::experimental::generator behavior)

    If set_exception is present, exception is stopped and given to the eventual consumer, for example a coroutine adapter for ppl tasks or std::future, defines set_exception to be packaged and passed to the consumer who calls .get() on the future/task.

     

  • User profile image
    Jacob

    hello everyone, how would I write a function like this with coroutines in c++?
    I would like to have a function that I can call over and over and it will generate a successive number.
    something like this...
    int gen(){

    int counter = 0;

    while(true){

    yield counter;

    counter++;

    }
    }

    int main(){

    gen(); // 0

    gen(); // 1 etc.

    }

    I am a c++ newbie, but the talk of coroutines, specifically the ones Gor is speaking of, are exciting!

  • User profile image
    AttilaKur

    First of all, thank you for the amazing work!

    But UpdateData() below fails because the continuation doesn't run on the awaiter thread (main gui thread). What do I customize in PPL to have an automatic thread context switch, please?

    ... DDX_Text(pDX, IDC_EDIT1, m_CalculationResult); ...

    int calculateInt()
    {
    ::Sleep(2000);
    return 42;
    }

    task<int> calcBigValueAsync()
    {
    return create_task([] { return calculateInt(); });
    }

    void MfcDialog::OnBnClickedBtnStartcalc()
    {
    OnBnClickedBtnStartcalcImpl();
    }

    task<void> MfcDialog::OnBnClickedBtnStartcalcImpl()
    {
    m_CalculationResult = co_await calcBigValueAsync();
    UpdateData(FALSE); // !!! background thread can't access IDC_EDIT1 editbox
    }

Add Your 2 Cents