如何在公共lisp中管理cpu资源和同步线程



我正在寻找一些关于公共lisp中线程的建议或良好实践。基本上,我试图用全局变量+时钟+(也设置为线程(来同步一些线程。我对连接进程、进程等待、生成互斥/生成锁、条件变量等不同概念有点困惑。我使用ccl和sbcl,所以我可能应该使用bordeaux线程,但这只是管理两者的一种方便方法。简而言之,我的代码是有效的,但当我添加一个线程,而不是共享cpu资源时,这个线程会增加150%以上。

;; for instance using CCL64 Version 1.12 DarwinX8664
;; --- THREADS-SET-1
(defvar +buffer+ nil)
(defvar +buffer-size+ 30)
(defparameter +compute+
(ccl:process-preset (ccl:make-process "+compute+")
#'(lambda ()
(loop do
(push (do-some-computation) +buffer+)
(sleep 0.1)))
'+compute+))
(defparameter +osc-send+
(ccl:process-preset (ccl:make-process "+osc-send+")
#'(lambda ()
(loop do
(when +buffer+
(OSCsend (car (last +buffer+)))
(setf +buffer+ (butlast +buffer+))
(if (> (length +buffer+) +buffer-size+)
(ccl:process-suspend +compute+)
(ccl:process-resume +compute+)))
(sleep (some-time))))
'+osc-send+))
;; commands:
(progn
(ccl:process-enable +compute+)
(sleep 1)
(ccl:process-enable +osc-send+))
(progn
(ccl:process-suspend +compute+)
(ccl:process-suspend +osc-send+))
(progn
(ccl:process-resume +compute+)
(ccl:process-resume +osc-send+))
(progn
(ccl:process-kill +compute+)
(ccl:process-kill +osc-send+))
;; when these threads as THREADS-SET-1 are 'playing' dx86cl64 takes more than 90% of cpu
;; and when I add some other threads as THREADS-SET-2, the cpu goes beyond 150%
;; needless to say that I did not try to add a third one before to solve this issue...

最后的想法是用+时钟+同步线程集-1和线程集-2。也许lparralel在这种情况下是相关的,如果是这样的话,感谢说明如何在这种情况中使用它。提前感谢您的任何帮助或任何参考书来了解这个主题。

最后,我为每个"例程"加一个"时钟"线程,解决了这个问题。然后,对于3个例程,cpu活动减少到50%左右。无论如何,如果有人能解释为什么在第一个例子中,线程占用了所有的cpu资源,而不是当我逐个例程处理一个线程时,我会很高兴。我怀疑使用进程挂起/恢复而不是互斥。。。接下来的事情!:(

最新更新