我写了一个剥离新线程的服务器。其中一些线程需要写入标准输出,但是当它们这样做时,终端中不会显示任何内容。
sbcl 中是否有某种类型的消息传递 api 允许我将消息发送回主线程?
多谢!
您需要
以某种方式将当前*standard-output*
传递给新线程,然后在该线程的函数中,您可以将*standard-output*
绑定到该值。
当前的Common Lisp实现使线程本地动态绑定,SBCL就是其中之一。
(sb-thread:make-thread ;; thread function
#'(lambda (standard-output)
;; thread-local dynamic binding of special variable
(let ((*standard-output* standard-output))
...))
;; thread function argument, provided by the current thread
:arguments (list *standard-output*))
请注意,我可以将线程函数的参数命名为*standard-output*
,然后我就不需要let
,因为动态绑定是在函数入口处完成的。 但我认为动态绑定应该是明确和明显的,尽管围绕特殊变量命名约定的耳罩。