2010-07-14 65 views
0

我正在嘗試使用單聲道在C#中使用微線程消息傳遞庫。從Mono 2.4開始,顯然在'Mono.Tasklets'下有延續性(不是收益率)。然而,這些缺少我能找到的任何文檔,雖然我通常使用它們,但偶爾會發生(但可再現)崩潰,調試器將不會附加到這些崩潰。崩潰使用Mono.Tasklets.Continuation

我的具體問題是:

有誰知道馬克()函數是什麼,在哪裏/當我需要調用它呢?它似乎是一次性初始化,但我不知道爲什麼那時不在構造函數中。

使用多個延續有限制嗎?我發現你不能將延續傳遞給另一個線程,但似乎我可以存儲多個延續並來回切換?

回答

2

LE:關於死機問題看到這些開放的錯誤:Bug 566324Bug 580791Bug 602502

我是相當新的這個我自己,我在這裏我迄今收集的信息。也許它會有用。 1)Mono.Tasklets庫(由Miguel de Icaza描述[閱讀:here])是一個連續庫,可用於構造各種形式的連續系統和輕量級(LW)線程。

一個簡單的方法來描述這是C的longjmp/setjmp的單語版本,它們只能用於展開堆棧。

庫最初是由一個人開發的[閱讀:here]現在它包含在單和記錄[閱讀:here(跟隨這些鏈接,你會發現更多的信息)

那傢伙實施在這個抽象之上的微線程庫。現在已移植到Mono.Tasklets框架

2)continuation是一個對象,可用於存儲當前的執行狀態,然後可用於稍後恢復存儲的狀態。

此處的「執行狀態」表示堆棧,其中包括調用堆棧和局部變量以及處理器的寄存器。

當存儲狀態恢復時,程序執行看起來像跳回到保存狀態的位置,並恢復所有局部變量。上Wikipedia/Continuations

3)API

An example in C.等詳細信息是:

public class Continuation { 
     public Continuation(); 
     public void Mark(); 
     public int Store (int state); 
     public void Restore (int state); 
} 

延續可用於實現微螺紋。你可以看看Mono的代碼。在Github上可用的微螺紋[READ:here]

public Continuation() 
    { 
     m_handle = alloc_continuation(); 
     Print("Continuation()"); 
    } 

    public void Mark() 
    { 
     Print("Mark()"); 
     // skip 1 frame, ie. this function 
     mark_continuation_frame(m_handle, 1); 
    } 

    public int Store(int data) 
    { 
     Print("Store({0})", data); 
     int res = store_continuation(m_handle, data); 
     Print("Store({0}) = {1}", data, res); 
     return res; 
    } 

    public void Restore(int data) 
    { 
     Print("Restore({0})", data); 
     restore_continuation(m_handle, data); 
     Print("Restore() exit (NEVER REACHED)"); 
    } 

從呈現什麼here

馬克()它用於標記的最頂層幀被存儲

Store(x)將當前狀態存儲到繼續,並返回給定的整數x。

還原(y)恢復存儲的狀態,並返回給定的整數y。 (注意:給恢復整數y實際上是從商店()方法返回,因爲這是我們的狀態有所恢復後)。

static void Main() 
{ 
Continuation c = new Continuation(); 
c.Mark(); 
int foo = 123; 
int val = c.Store(0); 
Console.WriteLine("{0} {1}", val, foo); 
foo = 321; 
if (val < 5) 
    c.Restore(val + 1); 
} 

當你調用存儲()的當前狀態執行被記錄下來並且可以通過調用Restore()返回到這個狀態。

呼叫者存儲()告訴它是否是基於結果從存儲初始存儲或還原點:

var c = new Continuation(); 
... 

switch (c.Store (0)){ 
case 0: 
     // First invocation 
case 1: 
     // Restored from the point ahead. 
} 
... 
// Jump back to the switch statement. 
c.Restore (1); 
+0

的monoco鏈接+特別是你的總結是極其有幫助 - 謝謝! – 2010-07-26 15:50:05