Re:Looking at Unity made me understand the point of C++ coro...
by spacechild1 on Saturday March 28, 2026 @11:20PM
(#47560111)
Any data that needs to survive across several callback invocations requires shared ownership. This means that the data must be allocated on the heap and probably also requires reference counting. With lambdas it's also easy to make mistakes, e.g. by capturing variables on the stack by reference.
In a coroutine everything can just live on the stack because the stack frame itself is kept alive across the asynchronous function calls.
Don't you see the big difference regarding lifetime management?
Re:Looking at Unity made me understand the point of C++ coro...
by mgaunard on Sunday March 29, 2026 @03:33PM
(#47566416)
Not necessarily, I already explained how (resource binding).
Close the socket, all handlers are cancelled, context can be freed immediately. The socket owns the data.
Re:Looking at Unity made me understand the point of C++ coro...
by spacechild1 on Sunday March 29, 2026 @04:21PM
(#47566869)
> The socket owns the data.
What if there's more than one socket involved?
Re:Looking at Unity made me understand the point of C++ coro...
by mgaunard on Tuesday March 31, 2026 @09:13AM
(#47586888)
Then you have it owned by the parent which owns all relevant sockets.
Resource ownership management 101.
Re:Looking at Unity made me understand the point of C++ coro...
by spacechild1 on Wednesday April 01, 2026 @02:10PM
(#47604423)
But that's not what you want. The context object should be freed when the associated operation finishes (either successfully or with an error), not when the parent goes out of scope. With coroutines I can simply put the context object on the stack and when the task finishes, the object automatically goes out of scope.
The default behaviour of many asynchronous systems is to extend the lifetime of context data until all the asynchronous handlers have run. You can also just bind them to the resource instead which is arguably more elegant, but which depends on how cancellation is implemented.