Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading SBCL螺纹中的死锁_Multithreading_Common Lisp_Sbcl - Fatal编程技术网

Multithreading SBCL螺纹中的死锁

Multithreading SBCL螺纹中的死锁,multithreading,common-lisp,sbcl,Multithreading,Common Lisp,Sbcl,我正在用SBCL为一个研究项目编写脚本,这是我第一次尝试SB-TREAHD。每个线程都会多次调用外部shell命令,其中使用了sb ext:run程序 问题是,每当出现ext:run程序时,该程序都可能会运行到死锁中(在死锁中,我没有使用任何类似互斥的东西)。我做了一段时间的实验,没有找到任何解决办法。代码的简化版本仍然可以使死锁发生,如下所示: (use-package :sb-thread) ;;; Global Settings (defparameter *path-num* 4) (

我正在用SBCL为一个研究项目编写脚本,这是我第一次尝试SB-TREAHD。每个线程都会多次调用外部shell命令,其中使用了sb ext:run程序

问题是,每当出现ext:run程序时,该程序都可能会运行到死锁中(在死锁中,我没有使用任何类似互斥的东西)。我做了一段时间的实验,没有找到任何解决办法。代码的简化版本仍然可以使死锁发生,如下所示:

(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)
其中在(main)中创建了4个线程,每个线程都将运行(第一次迭代),在(第一次迭代)中它调用(genconf)多次,这涉及(sb ext:run program)和文件I/O

我认为死锁可能是由于错误地使用了sb线程,但仅仅通过查看SBCL手册,我无法找到正确的方法。任何建议都会有帮助

顺便说一句,要运行该程序,请获取已创建所有必需目录/文件的文件


谢谢大家!

这对我来说并不重要。您使用的是什么版本的SBCL?我相信在过去的一年里,线程技术有了显著的改进。感谢您测试代码!我在大学工作站上使用sbcl 1.0.53,在debian笔记本电脑上使用sbcl 1.0.56。我想1.0.56是一个相当新的版本?但是我忘了说死锁并不是每次运行它时都会发生,但是根据我的实验,如果我将线程数设置为接近那台机器上的内核,死锁的可能性很高,超过三分之二。也许向sbcl用户邮件列表报告?