スレッドを生成する - lisp-cookbook-ja/common-lisp GitHub Wiki
スレッド ライブラリ portable-threads bordeaux-threads
スレッドを生成する
portable-threadsでの例
portable-threadsは主にネイティブスレッドを提供している処理系のコンパチブルレイヤです。 スレッドの生成には、 spawn-thread スレッドの生存の確認には、 thread-alive-p を利用します。
(defpackage :thread-test
(:use :cl :portable-threads))
(in-package :thread-test)
(progn
(print "Test start")
(print "Create thread")
(let ((th (spawn-thread "foo" (lambda ()
(let ((*standard-output* #.*standard-output*)) ;printの出力を一箇所に纏めるため
(print "Start thread")
(sleep 3)
(print "End thread"))))))
(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での例
(defpackage :thread-test
(:use :cl :bordeaux-threads))
(in-package :thread-test)
(progn
(print "Test start")
(print "Create thread")
(let ((th (make-thread (lambda ()
(let ((*standard-output* #.*standard-output*)) ;printの出力を一箇所に纏めるため
(print "Start thread")
(sleep 3)
(print "End thread")))
:name "foo")))
#+sbcl (join-thread th)
#-sbcl (loop :while (thread-alive-p th) :do (sleep 0.005))
(print "Test compleated")))
;-> "Test start"
; "Create thread"
; "Start thread"
; "End thread"
; "Test compleated"
;=> "Test compleated"