elisp thread-join 获取返回值问题

(defun mt-map (func sequence)
  (mapcar
   #'(lambda (elem)
      (make-thread
      #'(lambda () (apply func elem))))
   sequence)
  )

(mt-map #'1+ '(1 2 3))
;; => (#<thread 0x57f4550bdf08> #<thread 0x57f4550be110> #<thread 0x57f4550be318>)

(mapcar
 #'(lambda (thrd) (thread-join thrd))
 (mt-map #'1+ '(1 2 3)))
;; => (nil nil nil)

尝试写了一个多线程的例子,发现最后thread-join那里拿不到返回值,从文档看thread-join应该能够获取到线程的返回值,不知道哪里我理解的不对。求指点。

;; this also does not work!
(let ((thrd (make-thread
             '(lambda () (+1 2)))))
  (thread-join thrd))
;; => nil
(funcall (lambda () (+1 2)))
;; Lisp error: (invalid-function 1)

应为 (lambda () (1+ 2))

然后 mt-map 应为

(defun mt-map (func sequence)
  (mapcar
   (lambda (elem)
     (make-thread
       (lambda () (funcall func elem))))
   sequence))

(mapcar
 #'(lambda (thrd) (thread-join thrd))
 (mt-map #'1+ '(1 2 3)))
(2 3 4)

因为

(apply #'1+ 1)
;;; Lisp error: (wrong-type-argument listp 1)
1 个赞

是的,这正是问题所在,谢谢你!