我正在使用SBCL编写一个研究项目的脚本,这是我第一次尝试使用SB-TREAHD。每个线程将多次调用外部shell命令,为其使用sb-ext:run-program。
问题在于,无论何时出现ext:run-program,程序都会遇到死锁(我没有使用mutex这样的任何东西)。我试验了一段时间,找不到任何解决方案。仍然可以发生死锁的代码的简化版本如下:
(use-package :sb-thread)
;;; Global Settings
(defparameter *path-num* 4)
(defparameter *testing* nil)
(defparameter *training* nil)
(defparameter *shared-folder* "shared")
(defparameter *template* "template.conf")
(defparameter *pwd* (namestring (truename ".")))
;;; Utilities
(defmacro compose-file-name (&rest parts)
"compose a filename under current *pwd*"
`(concatenate 'string *pwd*
,@(mapcar (lambda (x) `(format nil "/~a" ,x))
parts)))
(defun run-command (command &optional args)
"run a shell comamnd and reflect the stdout on screen."
(let* ((process (sb-ext:run-program command args
:output :stream
:wait nil))
(output (sb-ext:process-output process)))
(loop for line = (read-line output nil)
while line do (format t "~a~%" line))))
(setf *testing* '("1" "2" "3" "4"))
(setf *training* '("5" "6" "7" "8"))
(defun gen-conf (path-id target labeled)
"Prepare the configuration file"
(format t "[~a]: ~a~%" path-id target)
(let ((current-dir (compose-file-name path-id)))
(run-command "/bin/cp" (list "-f" (compose-file-name *shared-folder* *template*)
(format nil "~a/Prediction.conf" current-dir)))
(with-open-file (*standard-output* (format nil "~a/Prediction.conf" current-dir)
:direction :output
:if-exists :append)
(format t "--estimate ~a~%" path-id))))
(defun first-iteration ()
(loop for i below 20
do (gen-conf (thread-name *current-thread*) (format nil "~a" i) (list "123" "456"))))
;;; main
(defun main ()
(let ((child-threads (loop for i below *path-num*
collect (make-thread
(lambda () (first-iteration))
:name (format nil "~a" i)))))
(loop for th in child-threads
do (join-thread th))))
(main)
其中in(主)4个线程被创建,并且每个线程将运行(第一次迭代),并且在(第一次迭代)中它多次调用(gen-conf),这涉及两者(sb-ext:run -program)和文件I / O.
我想也许死锁是因为错误地使用sb-thread,但仅仅通过查看SBCL手册我找不到正确的方法。任何建议都会有所帮助。
顺便说一下,要运行该程序,请获取http://pages.cs.wisc.edu/~breakds/thread.tar.gz,其中包含所有必需的目录/文件。
谢谢!