[armedbear-devel] CLOS thread safety

David Kirkman dkirkman at ucsd.edu
Sat Apr 24 18:12:34 UTC 2010


On Sat, Apr 24, 2010 at 9:44 AM, Tobias C. Rittweiler <tcr at freebits.de> wrote:
> There's a trick against the mangling. Use
>
>  (fresh-line)
>  (write-line (format nil <fmt-ctrl> . <fmt-args>))
>

That seems to have a much smaller but still non-vanishing
interaction cross section.  It's much better than just a plain
(format t ...), but it still occasionally mangles.

(defun print-stuff ()
  #'(lambda ()
      (dotimes (i 1000)
        (fresh-line)
        (write-line (format nil "i = ~A"  i) ))))

(dotimes (i 10)
  (threads:make-thread (print-stuff)))


Produces a few lines like these:

i = 831
i = 826i
= 832
i = 833
i = 828

In my actual code I use a sledge hammer and

(defvar *output-lock* (cons nil nil))

then I wrap anything that writes a message to the screen with

(with-thread-lock (*output-lock*)
  (format ...))

Of course, combining the two approaches and wrapping the
(write-line ...) would minimize thread contention, but about
the only thing I use this for is debug output ...

Thanks,

david




More information about the armedbear-devel mailing list