Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #67263 > unrolled thread

Why can't I understand what coroutines are?

Started byJuha Nieminen <nospam@thanks.invalid>
First post2019-08-04 09:37 +0000
Last post2019-08-05 07:46 +0000
Articles 12 on this page of 72 — 19 participants

Back to article view | Back to comp.lang.c++


Contents

  Why can't I understand what coroutines are? Juha Nieminen <nospam@thanks.invalid> - 2019-08-04 09:37 +0000
    Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-04 02:58 -0700
      Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-04 09:18 -0400
        Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-04 11:15 -0700
    Re: Why can't I understand what coroutines are? Louis Krupp <lkrupp@nospam.pssw.com.invalid> - 2019-08-04 04:58 -0600
      Re: Why can't I understand what coroutines are? Juha Nieminen <nospam@thanks.invalid> - 2019-08-05 08:31 +0000
        Re: Why can't I understand what coroutines are? Martijn van Buul <pino@dohd.org> - 2019-08-05 09:51 +0000
          Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-05 14:44 -0700
    Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-04 14:08 +0100
      Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-04 09:23 -0400
        Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-04 14:36 +0100
          Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-04 10:31 -0400
            Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-04 16:42 +0100
              Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-04 16:43 -0400
                Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-04 13:58 -0700
                  Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-04 17:36 -0400
                    Re: Why can't I understand what coroutines are? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-08-04 22:43 +0100
                    Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-04 22:52 +0100
                      Re: Why can't I understand what coroutines are? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-08-04 23:10 +0100
                      Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-04 21:32 -0400
                        Re: Why can't I understand what coroutines are? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-08-05 18:32 +0100
                          Re: Why can't I understand what coroutines are? scott@slp53.sl.home (Scott Lurndal) - 2019-08-05 17:48 +0000
                            Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-05 20:14 -0400
                            Re: Why can't I understand what coroutines are? leigh.v.johnston@googlemail.com - 2019-08-06 04:13 -0700
                              Re: Why can't I understand what coroutines are? leigh.v.johnston@googlemail.com - 2019-08-06 04:23 -0700
                                Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-06 22:19 +0100
                                  Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-06 15:19 -0700
                                    Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-07 00:01 +0100
                                      Re: Why can't I understand what coroutines are? scott@slp53.sl.home (Scott Lurndal) - 2019-08-06 23:52 +0000
                                      Re: Why can't I understand what coroutines are? Öö Tiib <ootiib@hot.ee> - 2019-08-06 16:57 -0700
                                        Re: Why can't I understand what coroutines are? scott@slp53.sl.home (Scott Lurndal) - 2019-08-07 14:41 +0000
                                          Re: Why can't I understand what coroutines are? Öö Tiib <ootiib@hot.ee> - 2019-08-08 07:10 -0700
                                    Re: Why can't I understand what coroutines are? scott@slp53.sl.home (Scott Lurndal) - 2019-08-06 23:49 +0000
                    Re: Why can't I understand what coroutines are? Robert Wessel <robertwessel2@yahoo.com> - 2019-08-04 22:04 -0500
                    Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-04 20:10 -0700
          Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-04 11:20 -0700
            Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-04 20:30 +0100
              Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-04 14:03 -0700
              Re: Why can't I understand what coroutines are? Paavo Helde <myfirstname@osa.pri.ee> - 2019-08-05 09:36 +0300
                Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-05 14:34 -0700
          Re: Why can't I understand what coroutines are? scott@slp53.sl.home (Scott Lurndal) - 2019-08-04 19:46 +0000
        Re: Why can't I understand what coroutines are? Juha Nieminen <nospam@thanks.invalid> - 2019-08-05 08:37 +0000
          Re: Why can't I understand what coroutines are? David Brown <david.brown@hesbynett.no> - 2019-08-05 12:06 +0200
            Re: Why can't I understand what coroutines are? Juha Nieminen <nospam@thanks.invalid> - 2019-08-05 12:20 +0000
              Re: Why can't I understand what coroutines are? David Brown <david.brown@hesbynett.no> - 2019-08-06 15:06 +0200
                Re: Why can't I understand what coroutines are? David Brown <david.brown@hesbynett.no> - 2019-08-06 15:18 +0200
            Re: Why can't I understand what coroutines are? Bonita Montero <Bonita.Montero@gmail.com> - 2019-08-06 13:58 +0200
    Re: Why can't I understand what coroutines are? Sam <sam@email-scan.com> - 2019-08-04 09:17 -0400
    Re: Why can't I understand what coroutines are? Robert Wessel <robertwessel2@yahoo.com> - 2019-08-04 10:12 -0500
      Re: Why can't I understand what coroutines are? Juha Nieminen <nospam@thanks.invalid> - 2019-08-05 08:42 +0000
        Re: Why can't I understand what coroutines are? Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-05 13:35 +0100
        Re: Why can't I understand what coroutines are? Bart <bc@freeuk.com> - 2019-08-05 20:19 +0100
        Re: Why can't I understand what coroutines are? Christian Gollwitzer <auriocus@gmx.de> - 2019-08-05 22:26 +0200
          Re: Why can't I understand what coroutines are? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-08-05 23:09 +0200
            Re: Why can't I understand what coroutines are? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-09 19:49 -0700
              Re: Why can't I understand what coroutines are? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-08-10 05:13 +0200
            Re: Why can't I understand what coroutines are? Christian Gollwitzer <auriocus@gmx.de> - 2019-08-10 08:23 +0200
              Re: Why can't I understand what coroutines are? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-08-10 10:43 +0200
                Re: Why can't I understand what coroutines are? Christian Gollwitzer <auriocus@gmx.de> - 2019-08-11 22:19 +0200
                  Re: Why can't I understand what coroutines are? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-14 08:25 -0700
          Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-10 00:19 -0700
          Re: Why can't I understand what coroutines are? Juha Nieminen <nospam@thanks.invalid> - 2019-08-11 21:16 +0000
        Re: Why can't I understand what coroutines are? "Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com> - 2019-08-05 23:43 -0700
        Re: Why can't I understand what coroutines are? Bonita Montero <Bonita.Montero@gmail.com> - 2019-08-10 10:50 +0200
    Re: Why can't I understand what coroutines are? Mr Flibble <flibbleREMOVETHISBIT@i42.co.uk> - 2019-08-04 17:57 +0100
    Re: Why can't I understand what coroutines are? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-08-05 01:11 +0200
      Re: Why can't I understand what coroutines are? Juha Nieminen <nospam@thanks.invalid> - 2019-08-05 08:50 +0000
        Re: Why can't I understand what coroutines are? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-08-05 16:59 +0200
      Re: Why can't I understand what coroutines are? Manfred <noname@add.invalid> - 2019-08-06 16:24 +0200
      Re: Why can't I understand what coroutines are? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2019-08-07 00:52 +0200
        Re: Why can't I understand what coroutines are? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-09 18:11 -0700
    Re: Why can't I understand what coroutines are? Martijn van Buul <pino@dohd.org> - 2019-08-05 07:46 +0000

Page 4 of 4 — ← Prev page 1 2 3 [4]


#67513

From"Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com>
Date2019-08-10 00:19 -0700
Message-ID<qilr6b$tbj$2@gioia.aioe.org>
In reply to#67319
On 8/5/2019 1:26 PM, Christian Gollwitzer wrote:
> Am 05.08.19 um 10:42 schrieb Juha Nieminen:
[...]
> Coroutine solution:
> 
> void bftraverse(Tree* root) {
>     co_yield(root->content);
>     if (root->left) bftraverse(root->left);
>     if (root->right) bftraverse(root->right);
> }

Looks nice to me.

[toc] | [prev] | [next] | [standalone]


#67573

FromJuha Nieminen <nospam@thanks.invalid>
Date2019-08-11 21:16 +0000
Message-ID<qiq0it$1n6k$1@adenine.netfront.net>
In reply to#67319
Christian Gollwitzer <auriocus@gmx.de> wrote:
> Coroutine solution:
> 
> void bftraverse(Tree* root) {
>    co_yield(root->content);
>    if (root->left) bftraverse(root->left);
>    if (root->right) bftraverse(root->right);
> }

Since, as far as I understand, C++20 coroutines are stackless, I don't
understand how that can work. What happens when a coroutine calls itself
recursively?

Does it create a new set of local variables with operator new every time
it calls itself recursively? That sounds like it would be horrendously
inefficient in all kinds of ways.

[toc] | [prev] | [next] | [standalone]


#67326

From"Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com>
Date2019-08-05 23:43 -0700
Message-ID<qib7jd$crj$1@gioia.aioe.org>
In reply to#67300
On 8/5/2019 1:42 AM, Juha Nieminen wrote:
> Robert Wessel <robertwessel2@yahoo.com> wrote:
>> The point of a coroutine is that it can "suspend" at some arbitrary
>> point in its logic (say a dozens subroutine calls and loops deep, plus
>> a bunch of state from local variables), and then continue from that
>> point (still those dozen subroutine calls and loops deep, with state
>> intact), rather than at the beginning of the routine again.
> 
>  From all the text and discussion out there I'm getting the picture that
> coroutines are a bit like lambda functions (ie. essentially stateful
> functors), with the difference that the coroutine can be "called" again
> in such a manner that it continues from where it "returned" last time,
> rather than always from the beginning (which would happen with lambdas).
> 
> Is that correct?
> 
> Is there a simple example of a situation where this is significantly
> beneficial compared to a lambda or an explicitly written functor
> (ie. a class with member variables and an operator())?
> 

Damn. I thought they were trying to get something like a standard 
getcontext/setcontext, or fibers.

[toc] | [prev] | [next] | [standalone]


#67516

FromBonita Montero <Bonita.Montero@gmail.com>
Date2019-08-10 10:50 +0200
Message-ID<qim0fs$8sb$1@news.albasani.net>
In reply to#67300
> From all the text and discussion out there I'm getting the picture that
> coroutines are a bit like lambda functions (ie. essentially stateful
> functors), with the difference that the coroutine can be "called" again
> in such a manner that it continues from where it "returned" last time,
> rather than always from the beginning (which would happen with lambdas).

The difference is that the coroutine can resume its work after
returning and the coroutine can maintain its state between returns.

[toc] | [prev] | [next] | [standalone]


#67275

FromMr Flibble <flibbleREMOVETHISBIT@i42.co.uk>
Date2019-08-04 17:57 +0100
Message-ID<O7E1F.72583$CV5.33842@fx02.fr7>
In reply to#67263
On 04/08/2019 10:37, Juha Nieminen wrote:
> C++20 will introduce support for coroutines. To this day, for reasons
> that are completely mysterious even to me, I have *absolutely no idea*
> what they are. I just don't get it.
> 
> I go to the Wikipedia page "Coroutine"... and it tells me nothing.
> 
> "Coroutines are computer program components that generalize subroutines
> for non-preemptive multitasking, by allowing execution to be suspended
> and resumed. Coroutines are well-suited for implementing familiar program
> components such as cooperative tasks, exceptions, event loops, iterators,
> infinite lists and pipes."
> 
> This tells me nothing. I don't get it.
> 
> "By contrast, coroutines can exit by calling other coroutines, which may
> later return to the point where they were invoked in the original
> coroutine; from the coroutine's point of view, it is not exiting but
> calling another coroutine."
> 
> Can exit by calling other coroutines... And then the execution later
> resumes from that calling point. This is not "exiting" but "yielding",
> whatever that's supposed to mean.
> 
> Sounds like a normal function call to me, but what do I know? I can't
> understand what this is saying. It's like it's speaking in legible
> English, yet it somehow is complete gibberish at the same time. I don't
> get it.
> 
> It then gives a pseudocode example of coroutines... that look exactly
> like a function calling another (which then just returns to the
> original caller).
> 
> ==========================================
> var q := new queue
> 
> coroutine produce
>      loop
>          while q is not full
>              create some new items
>              add the items to q
>          yield to consume
> 
> coroutine consume
>      loop
>          while q is not empty
>              remove some items from q
>              use the items
>          yield to produce
> ==========================================
> 
> This looks to me exactly like
> 
> ==========================================
> var q := new queue
> 
> coroutine produce
>      loop
>          while q is not full
>              create some new items
>              add the items to q
>          call consume
> 
> coroutine consume
>      while q is not empty
>          remove some items from q
>          use the items
>      return
> ==========================================
> 
> But what do I know? I don't get it.
> 
> Ok, Wikipedia is just a generic article about coroutines. Maybe
> https://en.cppreference.com/w/cpp/language/coroutines has a better
> explanation. It's done from the perspective of C++ in particular,
> after all.
> 
> "A coroutine is a function that can suspend execution to be resumed
> later. Coroutines are stackless: they suspend execution by returning
> to the caller. This allows for sequential code that executes
> asynchronously (e.g. to handle non-blocking I/O without explicit
> callbacks), and also supports algorithms on lazy-computed infinite
> sequences and other uses."
> 
> A function that can suspend execution to be resumed later...
> Yeah, I don't get it. Allows sequential code that executes
> asynchronously... What? Supports algorithms on lazy-computed infinite
> sequences... Ok, but what does this all mean? I don't get it.
> 
> The examples given are complete gibberish. I have *absolutely no
> idea* what something like this is supposed to mean:
> 
>      co_await async_write(socket, buffer(data, n));
> 
> co_await? What?
> 
> It seems that everybody else is awaiting (hah!) and praising coroutines
> like the second coming of Christ. Yet I just can't understand what they
> are, or how exactly they are used or useful. All web pages I can find
> are legible English that still somehow manages to be completely meaningless
> gibberish that tells me nothing.
> 

This is why coroutines should be banned: the average programmer simply 
doesn't understand them. This state of affairs is probably partly due to 
the pervasive understanding that the abstract machine has a stack.  C++ is 
complicated enough.

/Flibble

-- 
"Snakes didn't evolve, instead talking snakes with legs changed into 
snakes." - Rick C. Hodgin

“You won’t burn in hell. But be nice anyway.” – Ricky Gervais

“I see Atheists are fighting and killing each other again, over who 
doesn’t believe in any God the most. Oh, no..wait.. that never happens.” – 
Ricky Gervais

"Suppose it's all true, and you walk up to the pearly gates, and are 
confronted by God," Bryne asked on his show The Meaning of Life. "What 
will Stephen Fry say to him, her, or it?"
"I'd say, bone cancer in children? What's that about?" Fry replied.
"How dare you? How dare you create a world to which there is such misery 
that is not our fault. It's not right, it's utterly, utterly evil."
"Why should I respect a capricious, mean-minded, stupid God who creates a 
world that is so full of injustice and pain. That's what I would say."

[toc] | [prev] | [next] | [standalone]


#67291

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-08-05 01:11 +0200
Message-ID<qi7omo$v95$1@dont-email.me>
In reply to#67263
On 04.08.2019 11:37, Juha Nieminen wrote:
> C++20 will introduce support for coroutines. To this day, for reasons
> that are completely mysterious even to me, I have *absolutely no idea*
> what they are. I just don't get it.
> 
> I go to the Wikipedia page "Coroutine"... and it tells me nothing.

[snip]

I'll address the conceptual only.

When I saw this posting earlier today I thought I'd whip up a concrete 
example, like I implemented coroutines in the mid 1990's. However, I 
discovered that I would then be late for a dinner, so I put it on hold.

Coroutines are just cooperative multitasking, multitasking with a single 
thread of execution but multiple call stacks. When you CALL a coroutine 
you create a new instance, with its own stack. There is nothing like 
that in the standard library. When a coroutine TRANSFERs to another 
coroutine it's like a `longjmp`, except that `longjmp` was designed to 
jump up to an earlier point in a call chain, while a coroutine transfer 
jumps to a separate call chain.

As I recall you're familiar with 16-bit Windows programming.

In 16-bit Windows (Windows 3.x in the early 1990s) each program 
execution was a coroutine. When a program was launched, a stack area was 
allocated for it. That's a call of a coroutine. When the program called 
`GetMessage` or `Yield`, some other program instance would get a chance 
to continue to run (after waiting for /its/ call to `GetMessage` or 
`Yield` to return). That's a coroutine transfer.

The 16-bit Windows program execution were /huge/, heavy coroutines.

However, the main advantage of coroutines in ordinary programming is 
with coroutines as a very light-weight multitasking solution. Since 
there's only one thread of execution there are fewer synchronization 
issues. In particular one doesn't have to worry about whether one 
coroutine sees the memory changes effected by some other coroutine.


Cheers & hht.,

- Alf

[toc] | [prev] | [next] | [standalone]


#67301

FromJuha Nieminen <nospam@thanks.invalid>
Date2019-08-05 08:50 +0000
Message-ID<qi8qkc$7hv$1@adenine.netfront.net>
In reply to#67291
Alf P. Steinbach <alf.p.steinbach+usenet@gmail.com> wrote:
> Coroutines are just cooperative multitasking, multitasking with a single 
> thread of execution but multiple call stacks. When you CALL a coroutine 
> you create a new instance, with its own stack.

Does this mean that if a coroutine calls other functions normally (which
themselves may then call other functions and so on), this chain of calls
will use its own stack that's separate and independent from the stack
that's used by main() and everything it calls?

And if any of those functions along the chain "yields" (or whatever the
term was to "exit" this "pseudo-thread"), to later be returned to that
point of execution, all that stack is still there and this chain of
function calls will continue as before?

If that's the case, then your explanation gave me completely new insight
into coroutines that I didn't know nor understand before.

[toc] | [prev] | [next] | [standalone]


#67312

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-08-05 16:59 +0200
Message-ID<qi9g8f$r8v$1@dont-email.me>
In reply to#67301
On 05.08.2019 10:50, Juha Nieminen wrote:
> Alf P. Steinbach <alf.p.steinbach+usenet@gmail.com> wrote:
>> Coroutines are just cooperative multitasking, multitasking with a single
>> thread of execution but multiple call stacks. When you CALL a coroutine
>> you create a new instance, with its own stack.
> 
> Does this mean that if a coroutine calls other functions normally (which
> themselves may then call other functions and so on), this chain of calls
> will use its own stack that's separate and independent from the stack
> that's used by main() and everything it calls?

Conceptually, yes, and with a completely general implementation such as 
Windows fibers or the old Boost coroutines, or e.g. Modula-2 coroutines, 
yes.

However, C++ coroutines will be so called "stackless" coroutines as an 
optimization and restriction. According to ¹cppreference, "Coroutines 
are stackless: they suspend execution by returning to the caller". As I 
read it that means all coroutines using the C++20 syntax.

"Stackless" is possible by restricting the place that a coroutine 
transfer can be specified to the couroutine body itself, with no 
transfer in any code that it calls.

With this restriction one ensures that the coroutine's stack has a 
/very/ small maximum size when a transfer happens. AND its own 
parameters and expression evaluation is then all that it needs its own 
little stack for, because when a call out of the routine can't cause a 
transfer, then such a call will return with the stack that's used for 
the call, reverted back to the state at the call. Which means that calls 
out of the coroutine can be done on a stack that's common to all such 
"stackless" coroutines.

The restriction also plays another rôle: with C++20 coroutines a 
function is designated as a coroutine, i.e. that's the way that you 
specify that it is a coroutine, by containing one of the keywords that 
specify a transfer, namely any of `co_await`, `co_yield` or `co_return`.


> And if any of those functions along the chain "yields" (or whatever the
> term was to "exit" this "pseudo-thread"), to later be returned to that
> point of execution, all that stack is still there and this chain of
> function calls will continue as before?

Yes, with general coroutines.

But not with C++20 coroutines, due to the optimization & restriction.


> If that's the case, then your explanation gave me completely new insight
> into coroutines that I didn't know nor understand before.

Thanks.


Cheers!,

- Alf

Links:
¹ https://en.cppreference.com/w/cpp/language/coroutines

[toc] | [prev] | [next] | [standalone]


#67334

FromManfred <noname@add.invalid>
Date2019-08-06 16:24 +0200
Message-ID<qic2jf$9fn$1@gioia.aioe.org>
In reply to#67291
Excellent explanation, thanks!

On 8/5/2019 1:11 AM, Alf P. Steinbach wrote:
> On 04.08.2019 11:37, Juha Nieminen wrote:
>> C++20 will introduce support for coroutines. To this day, for reasons
>> that are completely mysterious even to me, I have *absolutely no idea*
>> what they are. I just don't get it.
>>
>> I go to the Wikipedia page "Coroutine"... and it tells me nothing.
> 
> [snip]
> 
> I'll address the conceptual only.
> 
> When I saw this posting earlier today I thought I'd whip up a concrete 
> example, like I implemented coroutines in the mid 1990's. However, I 
> discovered that I would then be late for a dinner, so I put it on hold.
> 
> Coroutines are just cooperative multitasking, multitasking with a single 
> thread of execution but multiple call stacks. When you CALL a coroutine 
> you create a new instance, with its own stack. There is nothing like 
> that in the standard library. When a coroutine TRANSFERs to another 
> coroutine it's like a `longjmp`, except that `longjmp` was designed to 
> jump up to an earlier point in a call chain, while a coroutine transfer 
> jumps to a separate call chain.
> 
> As I recall you're familiar with 16-bit Windows programming.
> 
> In 16-bit Windows (Windows 3.x in the early 1990s) each program 
> execution was a coroutine. When a program was launched, a stack area was 
> allocated for it. That's a call of a coroutine. When the program called 
> `GetMessage` or `Yield`, some other program instance would get a chance 
> to continue to run (after waiting for /its/ call to `GetMessage` or 
> `Yield` to return). That's a coroutine transfer.
> 
> The 16-bit Windows program execution were /huge/, heavy coroutines.
> 
> However, the main advantage of coroutines in ordinary programming is 
> with coroutines as a very light-weight multitasking solution. Since 
> there's only one thread of execution there are fewer synchronization 
> issues. In particular one doesn't have to worry about whether one 
> coroutine sees the memory changes effected by some other coroutine.
> 
> 
> Cheers & hht.,
> 
> - Alf

[toc] | [prev] | [next] | [standalone]


#67340

From"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>
Date2019-08-07 00:52 +0200
Message-ID<qid0bg$5oo$1@dont-email.me>
In reply to#67291
On 07.08.2019 00:38, Stefan Ram wrote:
> "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> writes:
>> I'll address the conceptual only.
> 
>    Not wanting to contradict anything,
>    just as an extension:
> 
>    A coroutine is a function that contains
>    either a coroutine-return-statement or
>    an await-expression or a yield-expression.

You mean, "A C++20 coroutine is...".

General implementations of coroutines do not have that or corresponding 
restriction.

Even if the restriction is a special case of coroutines, I think it can 
be argued that "coroutine" is a misnomer for the C++20 thing. :-) 
Because it implies much that isn't there.

   ---

By the way, I learned a bit more, by looking around at tutorials, and 
I'm appalled at all the machinery it seems one must define for the 
return type of even the simplest C++20 coroutine.

It's akin to the infamous 600+ line Microsoft OLE technology "Hello, 
world!"...

Hopefully they'll get around to provide some default machinery, before 
it's accepted in the standard.


Cheers!,

- Alf

[toc] | [prev] | [next] | [standalone]


#67508

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2019-08-09 18:11 -0700
Message-ID<86y301963r.fsf@mailhub.linuxsc.com>
In reply to#67340
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> writes:

> On 07.08.2019 00:38, Stefan Ram wrote:
>
>> "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> writes:
>>
>>> I'll address the conceptual only.
>>
>>    Not wanting to contradict anything,
>>    just as an extension:
>>
>>    A coroutine is a function that contains
>>    either a coroutine-return-statement or
>>    an await-expression or a yield-expression.
>
> You mean, "A C++20 coroutine is...".
>
> General implementations of coroutines do not have that or
> corresponding restriction.
>
> Even if the restriction is a special case of coroutines, I think
> it can be argued that "coroutine" is a misnomer for the C++20
> thing. :-)  [...]

Yes, and it's an easy argument to make.  The C++ construct I have
seen described bears almost no resemblance to coroutines as they
were originally described (I first read about them in Knuth v. 1).
A C++20 "coroutine" is more like a detachable, resumable function,
not at all like the symmetric arrangement of mutually calling
functions described in Knuth.

To be fair, it's probably worth mentioning that in those days (and
especially in Knuth's TAOCP), call linkage was a lot more static,
and what we now take for granted in the way of automatic storage
was often nowhere to be seen in many program environments.  Call
stack?  Local variables?  Reentrant functions?  These elements are
pretty much not found in environments where coroutines were
applied, and vice versa.  It's hard to unify these two kinds of
control structures.  So it isn't surprising that "coroutine" takes
on a rather different form in C++, compared to what coroutines
were originally.

[toc] | [prev] | [next] | [standalone]


#67297

FromMartijn van Buul <pino@dohd.org>
Date2019-08-05 07:46 +0000
Message-ID<5d47debc$0$2082$e4fe514c@news.kpn.nl>
In reply to#67263
* Juha Nieminen:
> C++20 will introduce support for coroutines. To this day, for reasons
> that are completely mysterious even to me, I have *absolutely no idea*
> what they are. I just don't get it.

I've been using lua in the past, and I was heavily using coroutines at
some point. I don't claim to be an expert on these matters, but I'll
tell you why *I* used them.

The key is in the following snippet :

> It then gives a pseudocode example of coroutines... that look exactly
> like a function calling another (which then just returns to the
> original caller).
>
>==========================================
> var q := new queue
>
> coroutine produce
>     loop
>         while q is not full
>             create some new items
>             add the items to q
>         yield to consume
>
> coroutine consume
>     loop
>         while q is not empty
>             remove some items from q
>             use the items
>         yield to produce


... except that the snippet threw away the child with the bath water, by
explicitly yielding to a specific target in both directions. That doesn't
add any real benefit indeed, but when done differently, coroutines offer
two benefits:

1) They decouple the consumer and the producer. 
2) You don't need to maintain state in an explicit container, you can
   just use local variables on the stack.

While coroutines certainly aren't necessary here ("But I can do it with a
function call to an interface method" is a straw man argument, as noone 
claimed otherwise - there are always multiple solutions to a single problem)
but they made some solutions certainly a lot more elegant.

I would suggest reading chapter 9 of "Programming in Lua". I know it's not
C++, but maybe it'll help as a primer. An older version is online at

https://www.lua.org/pil/9.1.html

-- 
Martijn van Buul - pino@dohd.org 

[toc] | [prev] | [standalone]


Page 4 of 4 — ← Prev page 1 2 3 [4]

Back to top | Article view | comp.lang.c++


csiph-web