Boost logo

Boost :

From: Andrzej Krzemienski (akrzemi1_at_[hidden])
Date: 2023-09-24 08:06:23


niedz., 24 wrz 2023 o 02:39 Klemens Morgenstern <
klemensdavidmorgenstern_at_[hidden]> napisał(a):

>
>
> On Sun, Sep 24, 2023, 3:51 AM Andrzej Krzemienski via Boost <
> boost_at_[hidden]> wrote:
>
>> Hi Everyone.
>> I am very uneasy about the dummy co_retuned value that I see in the
>> examples of async::generator. Like the one in the echo server example:
>> https://klemens.dev/async/tutorial.html#echo_server
>>
>> async::generator<tcp_socket> listen()
>> {
>> tcp_acceptor acceptor({co_await async::this_coro::executor}, {tcp::v4(),
>> 55555});
>> for (;;)
>> {
>> tcp_socket sock = co_await acceptor.async_accept();
>> co_yield std::move(sock);
>> }
>> co_return tcp_socket{acceptor.get_executor()}; // :-(
>> }
>>
>> The control will never get to the co_return. The caller never even tries
>> to
>> observe this value. and yet we are forced to return it.
>>
>
> You can throw a dummy exception, too. Never gets executed anyhow.
>
>>
>> Interestingly, the coroutine example from ASIO doesn't have this due to a
>> different design:
>>
>> https://www.boost.org/doc/libs/1_83_0/doc/html/boost_asio/example/cpp20/coroutines/echo_server.cpp
>>
>>
>>
>> std::generator doesn't have this.
>>
>> In other examples the degenerate value is used to indicate the end of
>> generation.
>>
>> I do not know the coroutines to be able to tell if this is a design
>> problem
>> with async::generator or with C++ coroutines in general. But it feels
>> wrong
>>
>
> Why? How would you want to communicate to the awaiter that the coro is
> done?
>

First, there are a bunch of use cases where the consumer of the generator
doesn't need to know, like the one with the listener: keep generating until
you are canceled.
Second, std::generator somehow does it. (I do not know how.)
Back to your question, when I see a code structure like this:

async::generator<T> fun()
{
  while(cond)
  {
    co_yield something;
  }
}

I know that there is nothing to do after the last co_yield in the loop. So
maybe the compiler/library should also.
Modulo that this may not be doable in the library, or the hacks are too
expensive.

Regards,
&rzej;

>
>
> that the end of generation should be signaled in this way.
>>
>> Regards,
>> &rzej;
>>
>> _______________________________________________
>> Unsubscribe & other changes:
>> http://lists.boost.org/mailman/listinfo.cgi/boost
>>
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk