返回   cpper编程论坛 > 技术杂烩
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

技术杂烩 找不到地方的技术问题?这里!

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2007-04-18
cat cat 当前离线
高级会员
 
注册日期: 2003-11-06
帖子: 1,563
文章: 6
cat 正向着好的方向发展
默认 Using C# 2.0 iterators to simplify writing asynchronous code

突然发现么有.net版了,老大看着转到最合适的版面吧。
.net 2.0里面有一个yield return,平时用得不多,听Jeffery Richter讲了一讲发现可以用来做这个,比较tricky
网上找了篇文章,先偷懒直接贴上来。不知道Jeffery课里面的内容是不是这位提供的
http://blogs.msdn.com/michen/archive...30/564671.aspx
http://blogs.msdn.com/michen/archive...01/566621.aspx

Using C# 2.0 iterators to simplify writing asynchronous code
A neat idea how C# 2.0 iterators can simplify the task of writing code that uses .NET async pattern.

I’ve recently attended Jeffrey Richter’s class dedicated to effective threading techniques, and that made me think about using the .NET async pattern. The typical usage of this pattern is the following: you start one or more asynchronous operations using obj.BeginOperation method (e.g. stream.BeginRead) passing a callback delegate and immediately return, releasing the thread that started processing. Once the async operation completes, your callback is called (usually on a thread-pool thread). The callback code checks what async operations have completed, and if you have enough data to proceed further it executes next block of code. This is very important for multithreaded servers, since very little resources are being used while the program waits for operation to finish (which can take a while).

But it is very hard to program with this pattern - instead of writing simple sequential code, the code is split into disconnected callbacks that may get executed in parallel. In some cases anonymous delegates make is somewhat better. But if more than one async operation is to be performed in sequence you usually need to maintain some state machine, evaluate where you are and what operation is to be performed next, etc - complex and buggy.

I thought it would be nice if there was a way to write sequential code that is put asleep (but does not waste thread), and then restarted when the async operation completes - so called coroutines (see http://en.wikipedia.org/wiki/Coroutines). Then I realized we already have these in C# 2.0 - in form of C# iterators! The idea is to organize the application code as an "iterator" that will "yield" some value after each async operation. Then we need a library utility class that restarts the iterator after the next async operation finishes.

The user code will looks like this:

代码:
IEnumerable<int> Foo(/* user params */..., /* utility class */ AsyncEnumerator ae) { // start async operation IAsyncResult asyncResult = stream.BeginRead(..., /* callback provided by utility*/ ae.Advance, null); // we could do some more stuff here before yielding yield return 1; // the code below is called after the code above // and after the asynchronous operation ends int dataRead = stream.EndRead(asyncResult); ... }
and is started by following code:

AsyncEnumerator ae = new AsyncEnumerator();
ae.Start(Foo(/* user params */ ..., ae));

The AsyncEnumerator utility guarantees the code is executed sequentially, although it may jump between threads . My iterator code originally yield the IAsyncResult returned by async operation it just started, but it was not really used by utility class. Jeffrey Richter suggested an ingenious idea - the iterator should produce the number of outstanding async operations started by the iterator code - and the utility class resumes the iterator when all of these operations have completed (surprisingly, this idea fits very well with the original synchronization code).

Another issue solved by the utility is exception handling: C# iterators can't have catch block around yield statement, so centralized error handling is hard. The utility catches exceptions raised by iterator, and calls a user-provided delegate that performs exception handling and returns boolean value indicating whether the iteration should stop (another neat improvement by Jeffrey Richter).

If this made you interested, come back to this blog - I'll post details on the inner working of the utility in the next entry.

References:
C# 2.0 iterators http://msdn.microsoft.com/msdnmag/issues/04/05/C20/

Async method pattern in .NET
http://msdn.microsoft.com/msdnmag/issues/01/08/Async/
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Published Thursday, March 30, 2006 1:44 AM by michen

Filed under: .NET

此帖于 2007-04-18 03:30 AM 被 cat 编辑.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2007-04-22
Elminster 的头像
超级版主
 
注册日期: 2002-09-09
帖子: 1,764
Elminster 正向着好的方向发展
默认

yield return 这种东西其实就是可以玩延迟求值,用 IEnumerate 来保存一个求值的承诺,然后等到合适的时候把这个值取回来。这个可以拿来玩很多东西,基本上就是 SICP 里面介绍的那个 stream 的性质。

8 过,我朋友已经成功地让我确信,Fiber + Async IO 才是解决这个问题的真正王道。可惜,.net 不打算支持 Fiber ,那天我已经和 Jeffery Ritcher 确认过了,sign ……
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2007-04-22
cat cat 当前离线
高级会员
 
注册日期: 2003-11-06
帖子: 1,563
文章: 6
cat 正向着好的方向发展
默认

你介绍一下你朋友怎么让你确信的吧~ 转一篇文章也好
这是一个fiber的sample code
http://msdn2.microsoft.com/en-us/library/ms686919.aspx

此帖于 2007-04-22 01:11 PM 被 cat 编辑.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2007-04-23
高级会员
 
注册日期: 2002-09-16
帖子: 1,087
文章: 1
SpitFire 正向着好的方向发展
默认 回复: Using C# 2.0 iterators to simplify writing asynchronous code

你们讨论的是不是这个东东?

http://home.macau.ctm.net/~kewei/you...ator-in-c.html
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2007-04-23
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,138
文章: 20
polyrandom 正向着好的方向发展
默认 回复: Using C# 2.0 iterators to simplify writing asynchronous code

我曾经见过有人用fiber把windows里面的enum callback转换为一个iterator,不过这样的方法实在不怎么好看。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用


相似的主题
主题 主题作者 版面 回复 最后发表
How To Organize Template Source Code zweily C/CPP/TMP/GP 14 2004-10-08 11:45 PM
有意思的循环 juyi 技术杂烩 3 2004-09-07 04:03 PM
Elmister's code abp 技术杂烩 4 2003-05-21 03:28 AM
Delphi vs Visual C++ -- Introduction Innocentius 技术杂烩 9 2002-10-11 12:05 PM


所有时间均为格林尼治时间 +9。现在的时间是 09:35 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2009,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0