我编写了一个服务器,用于生成新线程。其中一些线程需要写入标准输出,但当它们写入时,终端中不会显示任何内容。
sbcl 中是否有某种类型的消息传递 api 允许我将消息发送回主线程?
非常感谢!
最佳答案
您需要以某种方式将当前的*standard-output*
传递给新线程,然后在该线程的函数中,您可以将*standard-output*
绑定(bind)到该线程值(value)。
当前的 Common Lisp 实现进行线程本地动态绑定(bind),并且 SBCL is one of them .
(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
,因为动态绑定(bind)是在函数中完成的入口。但我认为动态绑定(bind)应该是明确和明显的,尽管围绕特殊变量命名约定的耳罩。
关于lisp - SBCL 多线程写入标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27178642/