[Openmcl-devel] Thread-local bindings

Ron Garret ron at flownet.com
Sat Jun 15 09:08:33 PDT 2019


On Jun 15, 2019, at 6:43 AM, martin <brooks.martin at sympatico.ca> wrote:

> — “Dynamic extent” refers to time within a single thread, and does have meaning across multiple threads.

My guess is that you meant to say that it does NOT have meaning across multiple threads, but this is not correct.  It does.  See below.

> — Funcall is a function; process-run-function is a special form.

No, they are both functions:

? #'funcall
#<Compiled-function FUNCALL #x30000007F9FF>
? #'process-run-function
#<Compiled-function PROCESS-RUN-FUNCTION #x300000401C7F>

Try this:

(defvar *foo* 'global-foo)
(defvar *cnt* 0)

(let ((lock (make-lock))
      (stream (if (find-package :hemlock-ext)
                (funcall (find-symbol "TOP-LISTENER-OUTPUT-STREAM" :hemlock-ext))
                *terminal-io*)))
  (defun out (s &rest args)
    (with-lock-grabbed (lock)
      (apply 'format stream s args)
      (terpri stream))))

(defun print-foo (thread-id)
  (dotimes (i 3)
    (out "~A ~A ~A" thread-id *foo* (incf *cnt*))
    (sleep 0.1)))

(defun test ()
  (process-run-function :thread-1 (lambda () (print-foo :thread1)))
  (process-run-function :thread-2 (lambda () (let ((*foo* :thread-2-foo))
                                               (print-foo :thread2))))
  (process-run-function :thread-2 (lambda () (let ((*foo* :thread-3-foo)
                                                   (*cnt* 0))
                                               (print-foo :thread2))))
  (print-foo :main-thread)
  (sleep 0.1)
  *cnt*)




More information about the Openmcl-devel mailing list