Thursday, March 31, 2011

Let over lambda saves the day (sbcl threads)

Messing with threads and sbcl/common lisp.

(loop for n from 1 to parallel-decision-num do
(format t "~&outsidethread~a" n)
(sleep 0.5)
(let ((m n))
(lambda ()
(sleep (random 5))
(format t "~&insidethread~a" m)
(sleep 2)))))

Remember to use a let so that the threads all have their own scope as you are spawning them. Also, join those mo-fos back up so that you don't cause horrible things.

(loop for thread in (sb-thread:list-all-threads)
(if (equal thread sb-thread:*current-thread*)
(sb-thread:join-thread thread)))

Probably not the best way to do it, but it is *a* way.

No comments: