2011-02-13 67 views
5

关于在维基百科的例子: http://en.wikipedia.org/wiki/Coroutine协程VS事件驱动编程

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 

我只是想知道传统的基于事件的方法可以处理这种使用模式,为什么需要使用协程?

回答

4

我认为它是“传统”的协同程序,事件是“现代”的。但是,他们也有不同的目的; AFAIK,协程可以指定何处传送控制权(如方法调用)或用于time-share,而事件是松散耦合通信(即在layered architecture中通信“向上”)。

如果您对这些事情感兴趣,一定要阅读Eric Lippert's blog series(2010年10月起)关于延续传球的风格。有一篇文章标题为“Musings about coroutines”。

+0

感谢您的博客推荐! – realjin 2014-08-25 08:50:25