2011-11-10 39 views
2

我試圖圍繞代理商撥打代理時發生的情況。代理商電話代理商

(def top (agent 0)) 
(def bottom (agent 1)) 

我有一個最小的一對:

(defn testA [] 
    "This returns 'top', whose value is 'bottom', whose value is 2." 
    (send top (fn [top-value] 
      (send bottom inc))) 
    (await top) 
    top) 

(defn testB [] 
    "This never terminates." 
    (send top (fn [top-value] 
      (send bottom inc) 
      (await bottom) ;;<- this is new 
      bottom)) 
    (await top) 
    top) 

什麼是與內部的await發生?當一個代理人打電話給另一個代理時,什麼因素起作用?

感謝,

回答

2

簡短的回答是,你不能在代理操作使用await。你可以看到這個錯誤(如果你打破目前的等待)與(agent-error top)

對於更長的答案(解釋你爲什麼不能這樣做),你將不得不(一)等一些clojure古茹:)我的是,你可以引入死鎖或其他一些災難。

另請注意,使用topbottom將返回代理本身,而不是其值。要獲得該值,您需要簡短地提供(deref top)@top