The Computer Language
Benchmarks Game

thread-ring Lisp SBCL program

source code

;;; The Computer Language Benchmarks Game
;;; contributed by Witali Kusnezow 2008-12-02

(defparameter *mutex* (sb-thread:make-mutex :name "lock"))

(defconstant  +threads+ 503)
(defparameter *counter* 0)
(defparameter *current* 0)
(defparameter *main-queue* (sb-thread:make-waitqueue))
(defparameter *queues*
  (make-array +threads+
              (loop for i of-type fixnum below +threads+
                 collect (sb-thread:make-waitqueue))))

(declaim (type fixnum *counter* *current*))

(defun thread-body ()
  (sb-thread:with-mutex (*mutex* :wait-p t)
    (let ((number *current*)
          (next (incf *current*)))
      (declare (fixnum number next))
      (if (= next +threads+) (setq next 0))
         with (curr_queue next_queue) =
           (list (svref *queues* number) (svref *queues* next))
         do (sb-thread:condition-wait curr_queue *mutex*)
         until (zerop *counter*)
         do (decf *counter*)  (sb-thread:condition-notify next_queue)
         finally (format t "~d~%" (1+ number))
           (sb-thread:condition-notify *main-queue*)))))

(defun start (n)
  (declare (type fixnum n))
  (setq *counter* n *current* 0)
  (let ((threads
            for i of-type fixnum below +threads+
            collect (sb-thread:make-thread #'thread-body))))
    (sb-thread:condition-notify (svref *queues* 0))
    (sb-thread:with-mutex (*mutex* :wait-p t)
      (sb-thread:condition-wait *main-queue* *mutex*))
    (dolist (i threads)
      (handler-case (sb-thread:terminate-thread i)
        (sb-thread:interrupt-thread-error () nil)))))

(defun main ()
  (let ((n (parse-integer (or (car (last #+sbcl sb-ext:*posix-argv*
                                         #+cmu  extensions:*command-line-strings*
                                         #+gcl  si::*command-args*)) "1"))))
    (start n)))

notes, command-line, and program output

32-bit Ubuntu one core
SBCL 1.3.10

Sat, 01 Oct 2016 06:07:38 GMT

cp: 'threadring.sbcl' and './threadring.sbcl' are the same file
SBCL built with: /usr/local/bin/sbcl --userinit /dev/null --batch --eval '(load "threadring.sbcl_compile")'
### START threadring.sbcl_compile
(handler-bind ((sb-ext:defconstant-uneql      (lambda (c) (abort c))))      (load (compile-file "threadring.sbcl" ))) (save-lisp-and-die "sbcl.core" :purify t)
### END threadring.sbcl_compile

; compiling file "/home/dunham/benchmarksgame/bench/threadring/threadring.sbcl" (written 24 JAN 2013 02:01:16 PM):
; compiling (DEFPARAMETER *MUTEX* ...)
; compiling (DEFCONSTANT +THREADS+ ...)
; compiling (DEFPARAMETER *COUNTER* ...)
; compiling (DEFPARAMETER *CURRENT* ...)
; compiling (DEFPARAMETER *MAIN-QUEUE* ...)
; compiling (DEFPARAMETER *QUEUES* ...)
; compiling (DECLAIM (TYPE FIXNUM ...))
; compiling (DEFUN THREAD-BODY ...)
; compiling (DEFUN START ...)
; compiling (DEFUN MAIN ...)

; /home/dunham/benchmarksgame_onecore/threadring/tmp/threadring.fasl written
; compilation finished in 0:00:00.022
[undoing binding stack and other enclosing state... done]
[saving current Lisp image into sbcl.core:
writing 3120 bytes from the read-only space at 0x1000000
writing 2304 bytes from the static space at 0x1100000
writing 28160000 bytes from the dynamic space at 0x9000000
### START threadring.sbcl_run
(main) (quit)
### END threadring.sbcl_run

0.61s to complete and log all make actions

/usr/local/bin/sbcl   --noinform --core sbcl.core --userinit /dev/null --load threadring.sbcl_run 500000


> 19


mmap: Cannot allocate memory
While evaluating the form starting at line 1, column 0
  of #P"/home/dunham/benchmarksgame_onecore/threadring/tmp/threadring.sbcl_run":

debugger invoked on a SIMPLE-ERROR in thread
#<THREAD "main thread" RUNNING {AAE0509}>:
  Could not create a new thread.