スレッドに引数を渡す - lisp-cookbook-ja/common-lisp GitHub Wiki

スレッド ライブラリ portable-threads bordeaux-threads

スレッドに引数を渡す

portable-threadsでの例

引数を渡してスレッドを生成するには spawn-thread の第3引数に渡します。

(defpackage :thread-test
  (:use :cl :portable-threads))
(in-package :thread-test)

(flet ((spawn-test (mesg)
         (let ((*standard-output* 
                #.*standard-output*)) ;printの出力を一箇所に纏めるため
           (print "Start thread")
           (print mesg)
           (sleep 3)
           (print "End thread"))))
  (print "Test start")
  (print "Create thread")
  (let ((th (spawn-thread "foo" #'spawn-test "こんにちは!!!")))
    (loop :while (thread-alive-p th) 
          :do (sleep 0.005)) ;wait FIXME joinとかないのだろうか。
    (print "Test compleated")))
;-> "Test start" 
    "Create thread" 
    "Start thread" 
    "こんにちは!!!" 
    "End thread" 
    "Test compleated" 
;=> "Test compleated" 

bordeaux-threadsでの例