Pavel Novikov - "Understanding Coroutines by Example" - C++ London

Sdílet
Vložit
  • čas přidán 8. 06. 2024
  • www.meetup.com/CppLondon/even...
    Slides: git.io/JJvLX
    Coroutines are a powerful tool added to C++20. Though currently there are basically no means to conveniently work with coroutines out of the box. That’s why we need to learn to properly use this tool to get benefits and to do it efficiently. We’ll be doing just that.
    We’ll figure out how to use coroutines from the ground up by example of how to work with asynchronous tasks.
  • Věda a technologie

Komentáře • 8

  • @w-mwijnja8919
    @w-mwijnja8919 Před 3 lety +2

    I very much enjoyed your Robert C. Weide joke at the beginning! :-D

  • @SamWhitlock
    @SamWhitlock Před 2 lety

    11:21 I know this is a simplification in the presentation, but this needs to be `return { coroutine_handle::from_promise(*this); }`. That effectively does the same thing, but it returns the coro handle (which the Task needs to hang on to as a member variable to preserve the coro).

    • @PaulSkeptic
      @PaulSkeptic Před 2 lety +1

      That's the other way to implement it. Coroutine handle and reference to promise are basically synonyms: having one you can trivially get the other (unless the coroutine handle is type erased like this: *std::coroutine_handle* ).
      In this case it was easier to use a pointer to promise instead of coroutine handle since *Promise* exposes functionality that the *Task* implementation can access directly without converting a coroutine handle to a reference to promise every time.

    • @SamWhitlock
      @SamWhitlock Před 2 lety +1

      @@PaulSkeptic Ah yes this makes sense. I'm still rewatching this presentation every week or so to learn a little more each time :D

  • @SamWhitlock
    @SamWhitlock Před 2 lety

    10:12 shouldn't this custom deleter also call `delete on the Promise pointer too?

    • @PaulSkeptic
      @PaulSkeptic Před 2 lety +1

      Promise "lives" inside the coroutine frame, so calling *.destroy()* on the coroutine _handle_ effectively destroys the coroutine frame _and_ the promise, freeing all the resources used by the coroutine.

  • @MaceUA
    @MaceUA Před 3 lety

    12:50 - can we just write `U&&` instead of that complex `decltype(...)` expression?

    • @PaulSkeptic
      @PaulSkeptic Před 3 lety +1

      Yes, you absolutely can.
      Expression inside decltype is just a copypaste of the expression in initialization.