From: Nikodemus S. <nik...@ra...> - 2005-08-22 04:32:35
|
On Sun, 21 Aug 2005, Christopher Neufeld wrote: > (defun main () > (dotimes (i 100) > (sb-thread:make-thread #'(lambda () (worker i))))) > The output of (main) should be the integers from 0 to 99, not necessarily > in order. Instead, a typical run will produce the number 13 seven times, > then the number 20 seven times, then the number 27 seven times, then the > number 33 twelve times, and so on. Finally, it will print the number 100 a > few times. This appears to be because by the time the closure is evaluated > to create the new thread, the value of 'i' has been incremented, maybe > several times. Right on mark up till this point. However, the solution is much simpler: (defun main () (dotimes (i 100) (let ((ti i)) (sb-thread:make-thread #'(lambda () (worker ti)))))) ...introduce a separate binding for each thread. (Untested, not having an x86 Linux handy.) Cheers, -- Nikodemus Schemer: "Buddha is small, clean, and serious." Lispnik: "Buddha is big, has hairy armpits, and laughs." |