2011-02-13 66 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