Discussion:
[asio-users] What is "passing the buck" pattern
hayate
2013-01-12 04:30:05 UTC
Permalink
Hi all,

I have read the ppt from Chris on the boostcon 2011. He mentioned a pattern
called "passing the buck" and said that "No reference counting; All memory
committed front up; Possibility of zero allocations in steady state"

If I have a connection class with 2 sockets inherited from
enable_shared_from<>, which is designed to be a proxy application.
1 .So how to apply this patter?
2. And since there are std::shared_ptr<> and std::bind out there, how can
it be zero allocations?

At last, are there some samples that demonstrate this pattern?

Thanks you guys
Amir Taaki
2013-01-12 13:15:43 UTC
Permalink
pass the buck invoke handlers after they complete.

there's a really good video by chris kohlhoff from boost con about this (i think it was on a random video site like blip.tv in 2009 boostcon) but i couldn't find it.

also i just found this instead which also looks good but i havent fully checked it out yet:

https://docs.google.com/viewer?a=v&q=cache:Y2wCe7bcOPIJ:https://raw.github.com/boostcon/2011_presentations/master/mon/thinking_asynchronously.pdf+asio+pass+the+buck&hl=en&gl=uk&pid=bl&srcid=ADGEESgMOTHJsGA3YhqIES8nrYC1p2cUlH0POO-4u0B53Td0Qy_FnN9Grq5yT_zwkJU3NVM2rzouIa3M9SPxW8_ub0rpJ5cHgEVqqARzrZY_ffv-jrD3hDZ1RFtNcS65t1MMUeFmwXda&sig=AHIEtbS4BfPWy-bJa5A0rmWmjx0akLPAtg




________________________________
From: hayate <***@gmail.com>
To: asio-***@lists.sourceforge.net
Sent: Saturday, January 12, 2013 5:30 AM
Subject: [asio-users] What is "passing the buck" pattern


Hi all,

I have read the ppt from Chris on the boostcon 2011. He mentioned a pattern called "passing the buck" and said that "No reference counting; All memory committed front up; Possibility of zero allocations in steady state"

If I have a connection class with 2 sockets inherited from enable_shared_from<>, which is designed to be a proxy application. 
1 .So how to apply this patter?
2. And since there are std::shared_ptr<> and std::bind out there, how can it be zero allocations?

At last, are there some samples that demonstrate this pattern?

Thanks you guys
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Amir Taaki
2013-01-12 13:18:53 UTC
Permalink
ahh here it is

http://blip.tv/boostcon/thinking-asynchronously-designing-applications-with-boost-asio-5250947




________________________________
From: Amir Taaki <***@yahoo.com>
To: "asio-***@lists.sourceforge.net" <asio-***@lists.sourceforge.net>
Sent: Saturday, January 12, 2013 2:15 PM
Subject: Re: [asio-users] What is "passing the buck" pattern


pass the buck invoke handlers after they complete.

there's a really good video by chris kohlhoff from boost con about this (i think it was on a random video site like blip.tv in 2009 boostcon) but i couldn't find it.

also i just found this instead which also looks good but i havent fully checked it out yet:

https://docs.google.com/viewer?a=v&q=cache:Y2wCe7bcOPIJ:https://raw.github.com/boostcon/2011_presentations/master/mon/thinking_asynchronously.pdf+asio+pass+the+buck&hl=en&gl=uk&pid=bl&srcid=ADGEESgMOTHJsGA3YhqIES8nrYC1p2cUlH0POO-4u0B53Td0Qy_FnN9Grq5yT_zwkJU3NVM2rzouIa3M9SPxW8_ub0rpJ5cHgEVqqARzrZY_ffv-jrD3hDZ1RFtNcS65t1MMUeFmwXda&sig=AHIEtbS4BfPWy-bJa5A0rmWmjx0akLPAtg




________________________________
From: hayate <***@gmail.com>
To: asio-***@lists.sourceforge.net
Sent: Saturday, January 12, 2013 5:30 AM
Subject: [asio-users] What is "passing the buck" pattern


Hi all,

I have read the ppt from Chris on the boostcon 2011. He mentioned a pattern called "passing the buck" and said that "No reference counting; All memory committed front up; Possibility of zero allocations in steady state"

If I have a connection class with 2 sockets inherited from enable_shared_from<>, which is designed to be a proxy application. 
1 .So how to apply this patter?
2. And since there are std::shared_ptr<> and std::bind out there, how can it be zero allocations?

At last, are there some samples that demonstrate this pattern?

Thanks you guys
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
asio-users mailing list
asio-***@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List
your project at
http://think-async.com/Asio/WhoIsUsingAsio
hayate
2013-01-13 07:08:48 UTC
Permalink
Hi Amir,
Thank you.

I have now read the pdf and watched the video, so this pattern is just what
all the async examples for asio try to show us, like async_read ->
async_write -> aync_read .... right?

I'm a little confused, is there any example that does not employ this
approch
Post by Amir Taaki
ahh here it is
http://blip.tv/boostcon/thinking-asynchronously-designing-applications-with-boost-asio-5250947
------------------------------
*Sent:* Saturday, January 12, 2013 2:15 PM
*Subject:* Re: [asio-users] What is "passing the buck" pattern
pass the buck invoke handlers after they complete.
there's a really good video by chris kohlhoff from boost con about this (i
think it was on a random video site like blip.tv in 2009 boostcon) but i
couldn't find it.
https://docs.google.com/viewer?a=v&q=cache:Y2wCe7bcOPIJ:https://raw.github.com/boostcon/2011_presentations/master/mon/thinking_asynchronously.pdf+asio+pass+the+buck&hl=en&gl=uk&pid=bl&srcid=ADGEESgMOTHJsGA3YhqIES8nrYC1p2cUlH0POO-4u0B53Td0Qy_FnN9Grq5yT_zwkJU3NVM2rzouIa3M9SPxW8_ub0rpJ5cHgEVqqARzrZY_ffv-jrD3hDZ1RFtNcS65t1MMUeFmwXda&sig=AHIEtbS4BfPWy-bJa5A0rmWmjx0akLPAtg
------------------------------
*Sent:* Saturday, January 12, 2013 5:30 AM
*Subject:* [asio-users] What is "passing the buck" pattern
Hi all,
I have read the ppt from Chris on the boostcon 2011. He mentioned a
pattern called "passing the buck" and said that "No reference counting; All
memory committed front up; Possibility of zero allocations in steady state"
If I have a connection class with 2 sockets inherited from
enable_shared_from<>, which is designed to be a proxy application.
1 .So how to apply this patter?
2. And since there are std::shared_ptr<> and std::bind out there, how can
it be zero allocations?
At last, are there some samples that demonstrate this pattern?
Thanks you guys
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET <http://asp.net/>, C#
2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
http://p.sf.net/sfu/learnmore_122912
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Marat Abrarov
2013-01-13 14:14:19 UTC
Permalink
I'm a little confused, is there any example that does not employ this approach
http://sourceforge.net/projects/asio-samples ?

Regards,
Marat Abrarov.
hayate
2013-01-13 16:10:02 UTC
Permalink
Hi Marat,

I'm reading your source, it's cool, but it is not that easy to
understand all the details at first sight because of so many optimizations
and tricks.

If I understand correctly, you do use "passing the buck" patterns heavily
in echo server sample, right? A complex state-machine to deal with
connections
Post by hayate
I'm a little confused, is there any example that does not employ this
approach
http://sourceforge.net/projects/asio-samples ?
Regards,
Marat Abrarov.
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
http://p.sf.net/sfu/learnmore_123012
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Marat Abrarov
2013-01-13 18:37:38 UTC
Permalink
Post by hayate
I'm reading your source, it's cool,
but it is not that easy to understand all the details at first sight
because of so many optimizations and tricks.
I want it to be the "next level" after official Asio examples. So it has as many of them (optimizations and tricks) as
it can contain.
Post by hayate
If I understand correctly, you do use "passing the buck" patterns heavily in echo server sample, right?
I do "passing the buck" almost everywhere - see classes
ma::(explicit_)context_alloc_handler (/include/ma/context_alloc_handler.hpp)
and
ma::(explicit_)context_wrapped_handler (/include/ma/context_wrapped_handler.hpp).
Post by hayate
A complex state-machine to deal with connections
Really complex. I'm looking for the way to make it simpler, still :)

Regards,
Marat Abrarov.
hayate
2013-01-14 02:54:11 UTC
Permalink
Hi Marat and others,

One more question, since I want to write a proxy application, it's very
similar to the example AsyncTransfer Chris present in the video, that means
in a steady state, proxy just transfers data between two sockets. I noted
that in Chris's presentation, the logic for one direction is:

async_read -> handle_read -> async_write -> handle_write... they are
sequential one by one, but I come up with this kind of one:

async_read -> handle_read -> async_write -> handle_write...
| --> async_read -> handle_read...

That means, in the handle_read function, an async_write call followed by an
async_read call. My idea is since the write IO may be slow, at the same
time maybe we can read more and process more.

Is this more concurrent?
Post by Marat Abrarov
Post by hayate
I'm reading your source, it's cool,
but it is not that easy to understand all the details at first sight
because of so many optimizations and tricks.
I want it to be the "next level" after official Asio examples. So it has
as many of them (optimizations and tricks) as
it can contain.
Post by hayate
If I understand correctly, you do use "passing the buck" patterns
heavily in echo server sample, right?
I do "passing the buck" almost everywhere - see classes
ma::(explicit_)context_alloc_handler
(/include/ma/context_alloc_handler.hpp)
and
ma::(explicit_)context_wrapped_handler
(/include/ma/context_wrapped_handler.hpp).
Post by hayate
A complex state-machine to deal with connections
Really complex. I'm looking for the way to make it simpler, still :)
Regards,
Marat Abrarov.
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
http://p.sf.net/sfu/learnmore_123012
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Yuri Timenkov
2013-01-14 14:12:05 UTC
Permalink
You can do this way, but remember 2 things:
1. Rate control. You need to suspend reading to allow write complete, if
the latter is much slower.
2. Buffer ownership. You need to keep in mind that write buffer shall be
untouched untill write completes, so you can't use same buffer for parallel
reading and writing.

Regards,
Yuri
Post by hayate
Hi Marat and others,
One more question, since I want to write a proxy application, it's very
similar to the example AsyncTransfer Chris present in the video, that means
in a steady state, proxy just transfers data between two sockets. I noted
async_read -> handle_read -> async_write -> handle_write... they are
async_read -> handle_read -> async_write -> handle_write...
| --> async_read -> handle_read...
That means, in the handle_read function, an async_write call followed by
an async_read call. My idea is since the write IO may be slow, at the same
time maybe we can read more and process more.
Is this more concurrent?
Post by Marat Abrarov
Post by hayate
I'm reading your source, it's cool,
but it is not that easy to understand all the details at first sight
because of so many optimizations and tricks.
I want it to be the "next level" after official Asio examples. So it has
as many of them (optimizations and tricks) as
it can contain.
Post by hayate
If I understand correctly, you do use "passing the buck" patterns
heavily in echo server sample, right?
I do "passing the buck" almost everywhere - see classes
ma::(explicit_)context_alloc_handler
(/include/ma/context_alloc_handler.hpp)
and
ma::(explicit_)context_wrapped_handler
(/include/ma/context_wrapped_handler.hpp).
Post by hayate
A complex state-machine to deal with connections
Really complex. I'm looking for the way to make it simpler, still :)
Regards,
Marat Abrarov.
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
http://p.sf.net/sfu/learnmore_123012
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Marat Abrarov
2013-01-14 16:17:42 UTC
Permalink
Post by hayate
async_read -> handle_read -> async_write -> handle_write...
| --> async_read -> handle_read...
That means, in the handle_read function, an async_write call followed by an async_read call.
My idea is since the write IO may be slow, at the same time maybe we can read more and process more.
echo_server in asio-samples does so. Theoretically it has to be more efficient. In practice (IMHO, in case of echo server. Can't measure, need high load) socket buffers (OS layer) almost level this with "async_read -> handle_read -> async_write -> handle_write -> async_read".
Post by hayate
1. Rate control. You need to suspend reading to allow write complete, if the latter is much slower.
2. Buffer ownership. You need to keep in mind that write buffer shall be untouched untill write completes,
so you can't use same buffer for parallel reading and writing.
For example, ma::echo::server::session uses ring buffer. When writes (reads) are slow then reads (writes) wait for available buffer space.

Regards,
Marat Abrarov.
hayate
2013-01-15 09:17:49 UTC
Permalink
thanks to all.

I will carefully study the echo_server sample
Post by hayate
Post by hayate
async_read -> handle_read -> async_write -> handle_write...
| --> async_read -> handle_read...
That means, in the handle_read function, an async_write call followed by
an async_read call.
Post by hayate
My idea is since the write IO may be slow, at the same time maybe we can
read more and process more.
echo_server in asio-samples does so. Theoretically it has to be more
efficient. In practice (IMHO, in case of echo server. Can't measure, need
high load) socket buffers (OS layer) almost level this with "async_read ->
handle_read -> async_write -> handle_write -> async_read".
Post by hayate
1. Rate control. You need to suspend reading to allow write complete, if
the latter is much slower.
Post by hayate
2. Buffer ownership. You need to keep in mind that write buffer shall be
untouched untill write completes,
Post by hayate
so you can't use same buffer for parallel reading and writing.
For example, ma::echo::server::session uses ring buffer. When writes
(reads) are slow then reads (writes) wait for available buffer space.
Regards,
Marat Abrarov.
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
Continue reading on narkive:
Loading...