Hello Christophe,

On Fri, Mar 12, 2010 at 08:27:15PM +0100, Christophe Grand wrote:

> See my memoize5: the call isn't computed inside the swap!s

That doesn't mean, that it is not computed several times!

user=> (defn f
         [x]
         (println "Got" x "from" (Thread/currentThread))
         (Thread/sleep 5000)
         (case x
           3 (f 2)
           2 (f 1)
           1 :done))
#'user/f
user=> (def f (memoize5 f))
#'user/f
user=> (-> #(do (f 3) (println "Done for" (Thread/currentThread))) Thread. 
.start)
       (Thread/sleep 2500)
       (-> #(do (f 3) (println "Done for" (Thread/currentThread))) Thread. 
.start)
Got 3 from #<Thread Thread[Thread-2,5,main]>
Got 3 from #<Thread Thread[Thread-3,5,main]>
Got 2 from #<Thread Thread[Thread-2,5,main]>
Got 2 from #<Thread Thread[Thread-3,5,main]>
Got 1 from #<Thread Thread[Thread-2,5,main]>
Got 1 from #<Thread Thread[Thread-3,5,main]>
Done for #<Thread Thread[Thread-2,5,main]>
Done for #<Thread Thread[Thread-3,5,main]>

Hmm? Wasn't f supposed to memoized? The problem is, that the call to f
is not guarded.

> Since you use a lock I think some clever combination of memoized functions
> can create a deadlock.

No. In the protected area we simply create a promise and fire off a
thread, which does the computation. Then we return immediatelly. So no
deadlock possibility here. However we trade one lock for the „other“
(the promise). What is the possibility of a deadlock here? Well, the
computation of f never completes. But this is not a problem of memoize.
The only way memoize could cause a problem here is that the computation
of f somehow calls f again with the same arguments. Then it would
deadlock on the promise, but without memoize we would also have a
infinite loop here...

Sincerely
Meikel

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to