Shammer's Philosophy

My private adversaria

Clozure CL で Thread を使ってみる【その4】

以下の記事では、

process-run-function を使用していたが、本来は make-process で Thread を作成し、Thread として実行する function を bind して、最後に Thread を開始する、というのが一番基礎となる実行方式のようだ。これをやる実行例を書いてみた。

(defun print-thread-name (count)
  (format t "~A:~A~%" *CURRENT-PROCESS* count))

(defparameter *count* 0)

(defparameter *lock* (make-lock))

(defparameter *stack* nil)

(push 'a *stack*)

(defun test-thread ()
  (loop
     (when (try-lock *lock*)
       (let ((permit (pop *stack*)))
	 (when (not (null permit))
	   (with-lock-grabbed (*lock*)
	     (print-thread-name *count*)
	     (push permit *stack*))
	   (sleep 1))))))


(defparameter *thread-01* (make-process "Thread-01"))

(process-preset *thread-01* #'test-thread)

(process-enable *thread-01*)

ただ延々と画面に文字を出力するだけの何も面白くないプログラムだが、make-process、process-preset、process-enable という基本的な流れになっているので念の為記録しておくことにしよう。