[armedbear-devel] CLOS thread safety

David Kirkman dkirkman at ucsd.edu
Mon Apr 26 18:36:46 UTC 2010

On Sat, Apr 24, 2010 at 9:17 AM, David Kirkman <dkirkman at ucsd.edu> wrote:
> On Sat, Apr 24, 2010 at 7:50 AM, Erik Huelsmann <ehuels at gmail.com> wrote:
>> Can you share some examples which show the issues here? Adding
>> thread-safety to clos.lisp - in specific, targeted places - should be
>> well doable.

Here is a second example that reliably gives me an error.  This
time, I only need two threads:

(defclass counter ()
  ((count :initform 0)))

(defun make-many (id)
    #'(lambda ()
        (dotimes (i 1000)
          (make-instance 'counter))
        (write-line (format nil "done with ~A" id))))

  (threads:make-thread (make-many 'thread1))
  (threads:make-thread (make-many 'thread2)))

This fails 100% of the time for me when I try to make 1000
instances, and about 25% of the time when I only try 100
instances, so if it does not give you an error try upping the
1000 by a factor of a few.  I'm using a dual core G5 right now,
java 1.5.0_19 (it fails with both the client and the sever VM)

I now think that the original example is also failing because of
make-instance, which is why I have to use so many threads
to get an error.


> It was surprisingly difficult to create a fragment which would reproduce the
> errors.  But here it is!
> (defclass counter ()
>  ((count :initform 0)))
> (setf *ncount* 10000)
> (setf *nthread* 1000)
> (defun count-thread (amount)
>  #'(lambda ()
>      (let ((c (make-instance 'counter)))
>        (dotimes (i *ncount*)
>          (incf (slot-value c 'count) amount))
>        (format t "count ~A = ~A~%" amount (slot-value c 'count)))))
> (dotimes (i *nthread*)
>  (threads:make-thread (count-thread i)))
> On my machine, a half dozen or so threads will reliably die with either:
> Missing slot MOP::NAME
> or
> The MOP::NAME error has about 5x more common than the
> MOP::PRECEDENCE-LIST error.  The key to reproducing the error is to use a
> lot of threads, with *nthread* = 2, the code can run all morning without any
> trouble.  But it will eventually give an error:  I originally noticed
> this with long
> running code and two threads.  Even with *nthread* = 10, it works without error
> most of the time.
> It also works (I mean gives errors) if you drop the final format in
> count-thread.
> The format is not thread safe, but it does not seem to cause errors by
> itself -- it
> just mangles output -- and it helps to see which threads are finishing.
> -david k.

More information about the armedbear-devel mailing list