【问题标题】:how to manage cpu resources and synchronise threads in common lispcommon lisp如何管理cpu资源和同步线程
【发布时间】:2020-12-12 23:38:15
【问题描述】:

我正在寻找一些关于 Common lisp 中线程的建议或良好实践。基本上,我正在尝试将一些线程与全局变量 +clock+ 同步(也设置为线程)。我对不同的概念有点困惑,比如加入过程、过程等待、make-mutex/make-lock、条件变量等。 我正在使用 ccl 和 sbcl,所以我应该使用 bordeaux-threads,但这只是管理两者的便捷方式。 简而言之,我的代码可以工作,但是当我添加一个线程而不是共享 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...

最后的想法是将 THREADS-SET-1 和 THREADS-SET-2 与 +clock+ 同步。 也许lparralel 在这种情况下可能是相关的,如果是这样,感谢说明如何在这种情况下使用它。提前感谢您提供任何帮助或任何参考书以了解此主题。

【问题讨论】:

  • 请注意,当您监控 cpu 使用率时,如果您有 8 个核心,您的百分比可能会超过 100% (unix.stackexchange.com/questions/145247/…)
  • 在 ccl:process-preset 周围有一对太多的括号,这看起来像一个函数 (f) 调用,它不是一个函数,即。 f = (ccl:process-preset ...);括号在 Lisp 中是有意义的
  • 抱歉,打错字了。
  • 我只有2个核心!

标签: multithreading common-lisp


【解决方案1】:

最后我解决了这个问题,为每个“例程”创建一个线程,为“时钟”创建一个线程。然后 CPU 活动下降到 3 个例程的 50% 左右。 无论如何,如果有人能解释为什么在第一个示例中线程占用所有 cpu 资源,而不是当我例行执行一个线程时,我会很高兴。我怀疑使用 process-suspend/resume 而不是互斥锁......接下来的事情! :)

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2022-01-16
    • 1970-01-01
    相关资源
    最近更新 更多