Skip to content

multiprocessing - race conditions

(defun test (message-count worker-count)
  (let ((to-workers (mp:make-semaphore))
        (from-workers (mp:make-semaphore)))
    (loop repeat worker-count
          do (mp:process-run-function
              "test"
              (lambda ()
                (loop
                   (mp:wait-on-semaphore to-workers)
                   (mp:signal-semaphore from-workers)))))
    (loop
       (loop repeat message-count
             do (mp:signal-semaphore to-workers))
       (loop repeat message-count
             do (mp:wait-on-semaphore from-workers))
       (assert (zerop (mp:semaphore-count to-workers)))
       (assert (zerop (mp:semaphore-count from-workers)))
       (format t ".")
       (finish-output))))

(defun run ()
  (test 10000 64))

RUN will eventually hang on all versions of ECL I've tried

Linux xi 3.2.0-24-generic-pae #39-Ubuntu SMP Mon May 21 18:54:21 UTC
2012 i686 i686 i386 GNU/Linux

(:NEW :LINUX :FORMATTER :ECL-WEAK-HASH :LITTLE-ENDIAN :ECL-READ-WRITE-LOCK
 :LONG-LONG :UINT64-T :UINT32-T :UINT16-T :RELATIVE-PACKAGE-NAMES :LONG-FLOAT
 :UNICODE :DFFI :CLOS-STREAMS :CMU-FORMAT :UNIX :ECL-PDE :DLOPEN :CLOS :THREADS
 :BOEHM-GC :ANSI-CL :COMMON-LISP :IEEE-FLOATING-POINT :PREFIXED-API :FFI
 :PENTIUM3 :COMMON :ECL)