220 5269 <CAH5h6+UnA0LWofyCS_XtHRPf-3vN=VXSEGZZd8_Z=1qjAGjyyA@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: minchul park <summerlight00@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: Some thoughts about coroutines
Date: Sat, 29 Jun 2013 10:02:58 +0900
Lines: 295
Approved: news@gmane.org
Message-ID: <CAH5h6+UnA0LWofyCS_XtHRPf-3vN=VXSEGZZd8_Z=1qjAGjyyA@mail.gmail.com>
References: <e4828fad-0428-46f8-ad6d-500848c2212b@isocpp.org>
	<291db835-d42d-40cd-8760-47cdf327680d@isocpp.org>
	<7d8a2d85-0995-4833-ad0c-75577f293b63@isocpp.org>
	<bca881ad-7d62-42d6-8e04-eb7320cebed1@isocpp.org>
	<2c77506f-433f-43b7-871f-e429c4b295bf@isocpp.org>
	<3e94ef89-4ea8-4549-827f-94dd7a32b9fa@isocpp.org>
	<1a1fd94c-eb51-4ff4-8e3d-7781a6c9b925@isocpp.org>
	<CAH5h6+V6s=n48y-NUBOzEQySEfZLsnLO3gqUjvxNkoCWpDKdkw@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=089e013c6a726a391c04e0408e39
X-Trace: ger.gmane.org 1372467779 18766 80.91.229.3 (29 Jun 2013 01:02:59 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Sat, 29 Jun 2013 01:02:59 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDH3HYO5YQIBBQ7EXCHAKGQE3W3X3JI@isocpp.org Sat Jun 29 03:03:01 2013
Return-path: <std-proposals+bncBDH3HYO5YQIBBQ7EXCHAKGQE3W3X3JI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ob0-f197.google.com ([209.85.214.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDH3HYO5YQIBBQ7EXCHAKGQE3W3X3JI@isocpp.org>)
	id 1UsjZ6-0003rw-Nl
	for gclcip-std-proposals@m.gmane.org; Sat, 29 Jun 2013 03:03:01 +0200
Original-Received: by mail-ob0-f197.google.com with SMTP id v19sf9079447obq.0
        for <gclcip-std-proposals@m.gmane.org>; Fri, 28 Jun 2013 18:02:59 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=x-beenthere:mime-version:in-reply-to:references:date:message-id
         :subject:from:to:x-original-sender:x-original-authentication-results
         :reply-to:precedence:mailing-list:list-id:x-google-group-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe
         :content-type;
        bh=/2sFH1JDZpP0nnSxr5FJvSyyUKVLZwJ6vsoKRsuCcRc=;
        b=lzZZx4pj1eHjW5UnVmJHdDFFSbL1Rq4kmk0dkKipQ5SYCcf9s9jZ/JWkqcbXTQtE2n
         xxMxBgCauehSLxLeLRBX950QL4puhyGG0fTBUcuGgHixZ2HYbfCmVl7WgDieqpqgfPvQ
         8mvMJPfzsdYKxMPk2Dkl3C9ssKFAcJDnPPX7OKzfDH9n/JYnTuBNrDK49kuAlKbtwrp1
         XbG4Ak3S0snkBovDv89IMbuZXvj3hUesnaUcjOPBdR5l45Y7rXeb4VyQt2ow42cH0Zc0
         /zEwYBCNGEieUiPzhjrhe1+dHbsi7BJpwuLtu/BCCNCVdsQlVrmZsd1TnSE84ygXe3HD
         9/fw==
X-Received: by 10.182.241.161 with SMTP id wj1mr4432773obc.3.1372467779623;
        Fri, 28 Jun 2013 18:02:59 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.182.103.164 with SMTP id fx4ls253288obb.44.gmail; Fri, 28 Jun
 2013 18:02:59 -0700 (PDT)
X-Received: by 10.182.79.68 with SMTP id h4mr7441589obx.68.1372467779102;
        Fri, 28 Jun 2013 18:02:59 -0700 (PDT)
Original-Received: from mail-ie0-x230.google.com (mail-ie0-x230.google.com [2607:f8b0:4001:c03::230])
        by mx.google.com with ESMTPS id kk1si2407842oeb.155.2013.06.28.18.02.59
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Fri, 28 Jun 2013 18:02:59 -0700 (PDT)
Received-SPF: pass (google.com: domain of summerlight00@gmail.com designates 2607:f8b0:4001:c03::230 as permitted sender) client-ip=2607:f8b0:4001:c03::230;
Original-Received: by mail-ie0-f176.google.com with SMTP id ar20so5331637iec.21
        for <std-proposals@isocpp.org>; Fri, 28 Jun 2013 18:02:58 -0700 (PDT)
X-Received: by 10.50.3.42 with SMTP id 10mr6323351igz.39.1372467778695; Fri,
 28 Jun 2013 18:02:58 -0700 (PDT)
Original-Received: by 10.64.62.200 with HTTP; Fri, 28 Jun 2013 18:02:58 -0700 (PDT)
In-Reply-To: <CAH5h6+V6s=n48y-NUBOzEQySEfZLsnLO3gqUjvxNkoCWpDKdkw@mail.gmail.com>
X-Original-Sender: summerlight00@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of summerlight00@gmail.com designates 2607:f8b0:4001:c03::230 as
 permitted sender) smtp.mail=summerlight00@gmail.com;       dkim=pass header.i=@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:5269
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/5269>

--089e013c6a726a391c04e0408e39
Content-Type: text/plain; charset=ISO-8859-1

Oops, the last code should be fixed as below.

void some_protocol(end_point dest) {
  session s(dest);

  // some processing
  s.send(buffer);
  s.recv(buffer); // deep yielded inside of network library

  // use received data
}


On Sat, Jun 29, 2013 at 9:55 AM, minchul park <summerlight00@gmail.com>wrote:

> For the sake of clarity, it's worth to note that the second proposal is a
> description of generator, rather than coroutine. They should be
> distinguished.
>
> Coroutine is not just about a simple programming pattern like
> producer/consumer (which is actually external iterator) - It's more
> primitive building block to support every kind of cooperative multitasking,
> so it should maintain its own continuation. So if you want to call
> something coroutine, it should have its own callstack(which represents
> continuation in C++).
>
> Of course, generator has its own advantage. It has much easier to use, and
> maybe more efficient on its primary use cases since it's just simple
> syntax-sugared state machine functor. Since iterator pattern is widely
> adapted in C++ community, it deserves its own syntax. So I'm not against
> about generator proposal, but still, it can't be a replacement of coroutine.
>
>
> And I guess you have doubt about necessity of deep yield. Think about
> simple IO scenario. Maybe the most simple pseudo code may looks like below.
>
> void some_protocol(end_point dest) {
>   session s(dest);
>   // some processing
>   s.send(buffer);
>   s.recv(buffer); // unbounded blocking
>
>   // use received data
> }
>
> But the code has some performance issue. One recv call can blocks other
> computation. It can be resolved by mapping one thread to one session , but
> it has its own performance problem. So let's introduce some asynchronous
> code.
>
> end_point dest;
> session s(dest); // connected and maintained by some other code - its
> lifetime is not bounded to the scope.
>
> void async_recv_handler(session& s) {
>   // use received data
> }
>
> void async_protocol(session& s) {
>   // some processing
>   s.send(buffer);
>   s.recv(recv_handler);
> }
>
> It's much more complex due to the nature of asynchronous process. Maybe
> CPS with lambda can mitigates it, but it's still ugly. However, if we have
> "deep yield" backed up by coroutine-based cooperative task scheduler, then
> the code becomes drastically simpler.
>
> void some_protocol(end_point dest) {
>   session s(dest);
>
>   s.recv(buffer); // deep yielded inside of network library
>
>   // use received data
> }
>
>
> The root cause of the problem is strong coupling between continuation and
> thread. We want to map one continuation to one conceptual computation
> context, but in C++11, thread is the only way to achieve it since they are
> strongly coupled.
>
> The fundamental solution is to decouple continuation from thread and treat
> continuation as first-class object. Though it's very hard (or technically
> infeasible) to implement it as a first class object in the context of C++,
> but coroutine can be the practically sufficient solution which can be
> applied almost every cases. The necessity of "deep yield" means this.
>
> I also want to note that generator just maintains only its own function
> frame, which is not sufficient to represent continuation in general use
> case.
>
>
> On Fri, Jun 28, 2013 at 10:10 AM, <asaelr@gmail.com> wrote:
>
>> Another thing - what should *operator()* return when the coroutine is
>> done? We can't know it's done before the internal function actually ends,
>> so we can't just check *isdone() *(or, maybe, *operator bool()* ) to
>> know if we shouldn't call *operator()*.
>> (One solution is to let the coroutine not to return control just when
>> yielding, but instead continue running until just before the next *return
>> * statement, but I think it's kind of evil)
>>
>>  --
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "ISO C++ Standard - Future Proposals" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to std-proposals+unsubscribe@isocpp.org.
>> To post to this group, send email to std-proposals@isocpp.org.
>> Visit this group at
>> http://groups.google.com/a/isocpp.org/group/std-proposals/.
>>
>>
>>
>
>
>
> --
> Min-chul Park (summerlight00@gmail.com / http://summerlight.tistory.com)
>



-- 
Min-chul Park (summerlight00@gmail.com / http://summerlight.tistory.com)

-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.



--089e013c6a726a391c04e0408e39
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div style=3D"font-family:arial,sans-serif;font-size:13px"=
>Oops, the last code should be fixed as below.</div><div style=3D"font-fami=
ly:arial,sans-serif;font-size:13px"><br></div><div style=3D"font-family:ari=
al,sans-serif;font-size:13px">
void some_protocol(end_point dest) {</div><div style=3D"font-family:arial,s=
ans-serif;font-size:13px">=A0 session s(dest);</div><div style=3D"font-fami=
ly:arial,sans-serif;font-size:13px"><br></div><div style=3D"font-family:ari=
al,sans-serif;font-size:13px">
<div>=A0 // some processing</div><div>=A0 s.send(buffer);</div></div><div s=
tyle=3D"font-family:arial,sans-serif;font-size:13px">=A0 s.recv(buffer); //=
 deep yielded inside of network library<br></div><div style=3D"font-family:=
arial,sans-serif;font-size:13px">
<br></div><div style=3D"font-family:arial,sans-serif;font-size:13px">=A0 //=
 use received data</div><div style=3D"font-family:arial,sans-serif;font-siz=
e:13px">}</div></div><div class=3D"gmail_extra"><br><br><div class=3D"gmail=
_quote">
On Sat, Jun 29, 2013 at 9:55 AM, minchul park <span dir=3D"ltr">&lt;<a href=
=3D"mailto:summerlight00@gmail.com" target=3D"_blank">summerlight00@gmail.c=
om</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"marg=
in:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir=3D"ltr">For the sake of clarity, it&#39;s worth to note that the s=
econd proposal is a description of generator, rather than coroutine. They s=
hould be distinguished.<div><br></div><div>Coroutine is not just about a si=
mple programming pattern like producer/consumer (which is actually external=
 iterator) - It&#39;s more primitive building block to support every kind o=
f cooperative multitasking, so it should maintain its own continuation. So =
if you want to call something coroutine, it should have its own callstack(w=
hich represents continuation in C++).</div>

<div><br></div><div>Of course, generator has its own advantage. It has much=
 easier to use, and maybe more efficient on its primary use cases since it&=
#39;s just simple syntax-sugared state machine functor. Since iterator patt=
ern is widely adapted in C++ community, it deserves its own syntax. So I&#3=
9;m not against about generator proposal, but still, it can&#39;t be a repl=
acement of coroutine.<br>

<div><br></div><div><br></div><div>And I guess you have doubt about necessi=
ty of deep yield. Think about simple IO scenario. Maybe the most simple pse=
udo code may looks like below.</div></div><div><br></div><div>void some_pro=
tocol(end_point dest) {</div>

<div>=A0 session s(dest);</div><div>=A0 // some processing</div><div>=A0 s.=
send(buffer);</div><div>=A0 s.recv(buffer); // unbounded blocking<br></div>=
<div><br></div><div>=A0 // use received data</div>
<div>}</div><div><br></div><div>But the code has some performance issue. On=
e recv call can blocks other computation. It can be resolved by mapping one=
 thread to one session , but it has its own performance problem. So let&#39=
;s introduce some asynchronous code.</div>

<div><br></div><div>end_point dest;</div><div>session s(dest); // connected=
 and maintained by some other code - its lifetime is not bounded to the sco=
pe.</div><div><br></div><div>void async_recv_handler(session&amp; s) {<br>

</div><div>=A0 // use received data</div><div>}</div><div><br></div><div>vo=
id async_protocol(session&amp; s) {</div><div><div>=A0 // some processing</=
div><div>=A0 s.send(buffer);</div></div><div>
=A0 s.recv(recv_handler);</div><div>}</div><div><br></div><div>It&#39;s muc=
h more complex due to the nature of asynchronous process. Maybe CPS with la=
mbda can mitigates it, but it&#39;s still ugly. However, if we have &quot;d=
eep yield&quot; backed up by coroutine-based cooperative task scheduler, th=
en the code becomes drastically simpler.</div>

<div><br></div><div><div>void some_protocol(end_point dest) {</div><div>=A0=
 session s(dest);</div><div><br></div><div>=A0 s.recv(buffer); // deep yiel=
ded inside of network library<br></div><div><br></div><div>=A0 // use recei=
ved data</div>

<div>}</div><div><br></div><div><br></div><div>The root cause of the proble=
m is strong coupling between continuation and thread. We want to map one co=
ntinuation to one conceptual computation context, but in C++11, thread is t=
he only way to achieve it since they are strongly coupled.</div>

<div><br></div><div>The fundamental solution is to decouple continuation fr=
om thread and treat continuation as first-class object. Though it&#39;s ver=
y hard (or technically infeasible) to implement it as a first class object =
in the context of C++, but coroutine can be the practically sufficient solu=
tion which can be applied almost every cases. The necessity of &quot;deep y=
ield&quot; means this.</div>

<div><br></div><div>I also want to note that generator just maintains only =
its own function frame, which is not sufficient to represent continuation i=
n general use case.=A0</div></div></div><div class=3D"gmail_extra"><div><di=
v class=3D"h5">

<br><br><div class=3D"gmail_quote">On Fri, Jun 28, 2013 at 10:10 AM,  <span=
 dir=3D"ltr">&lt;<a href=3D"mailto:asaelr@gmail.com" target=3D"_blank">asae=
lr@gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" sty=
le=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Another thing - what should <i>operator()</i> return when the coroutine is =
done? We can&#39;t know it&#39;s done before the internal function actually=
 ends, so we can&#39;t just check <i>isdone() </i>(or, maybe, <i>operator b=
ool()</i> ) to know if we shouldn&#39;t call <i>operator()</i>.<br>

(One solution is to let the coroutine not to return control just when yield=
ing, but instead continue running until just before the next <i>return</i> =
statement, but I think it&#39;s kind of evil)<div><div>
<br>

<p></p>

-- <br>
=A0<br>
--- <br>
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br>
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals%2Bunsubscribe@isocpp.org" target=3D=
"_blank">std-proposals+unsubscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" target=3D"_blank">std-proposals@isocpp.org</a>.<br>
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
=A0<br>
=A0<br>
</div></div></blockquote></div><br><br clear=3D"all"><div><br></div></div><=
/div><span class=3D"HOEnZb"><font color=3D"#888888">-- <br><div>Min-chul Pa=
rk (<a href=3D"mailto:summerlight00@gmail.com" target=3D"_blank">summerligh=
t00@gmail.com</a>=A0/ <a href=3D"http://summerlight.tistory.com/" target=3D=
"_blank">http://summerlight.tistory.com</a>)</div>


</font></span></div>
</blockquote></div><br><br clear=3D"all"><div><br></div>-- <br><div>Min-chu=
l Park (<a href=3D"mailto:summerlight00@gmail.com" target=3D"_blank">summer=
light00@gmail.com</a>=A0/ <a href=3D"http://summerlight.tistory.com/" targe=
t=3D"_blank">http://summerlight.tistory.com</a>)</div>

</div>

<p></p>

-- <br />
&nbsp;<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />
&nbsp;<br />
&nbsp;<br />

--089e013c6a726a391c04e0408e39--

.
