方案中的线程挂起/线程恢复

时间:2013-01-26 21:11:34

标签: multithreading scheme racket

我正在编写一个计划程序,用于在用户点击时创建“行星”的任务,并在单击复选框时启动/停止行星相互绕行。我们应该用一个线程来实现它。但是,当我单击复选框时,线程挂起似乎不起作用,但是恢复确实有效。

感谢您提供的任何帮助!这是代码:

#lang racket

(require racket/gui)
(require racket/block)

;; Small 2d vector library for the Newtonian physics
(define (x v) (vector-ref v 0))
(define (y v) (vector-ref v 1))
(define (x! v value) (vector-set! v 0 value))
(define (y! v value) (vector-set! v 1 value))
(define (v* v value) (vector-map (lambda (x) (* x value)) v))
(define (v+ v w) (vector-map + v w))
(define (v- v w) (vector-map - v w))
(define (v-zero! v) (vector-map! (lambda (x) 0) v))
(define (v-dot v w) (let ((vw (vector-map * v w))) (+ (x vw) (y vw))))
(define (v-mag v) (sqrt (v-dot v v)))
(define sem (make-semaphore))


;; Planet object
(define planet%
  (class object%
(public m p v calculate-force move draw)
(init-field (mass 1)
            (position (vector 0 0 ))
            (velocity (vector 0 0 ))
            (force (vector 0 0 )))
(define (m) mass)
(define (p) position)
(define (v) velocity)
;; Use Newton's law of gravitation.
;; I assume the gravitational constant is one
(define (calculate-force planet-list)
  (v-zero! force)
  (for-each (lambda (other-planet)
              (when (not (equal? this other-planet))
                (let* ((direction (v- (send other-planet p) position))
                       (dist (max 1 (v-mag direction)))
                       (other-mass (send other-planet m))
                       (new-force (v* direction (/ (* mass other-mass) (* dist dist))))
                      )
                  (vector-map! + force new-force))))
            planet-list)
  )
;; Simple Euler integration of acceleration and velocity
(define (move) 
  (let ((acc (v* force (/ 1.0 mass))))
    (vector-map! + velocity acc)
    (vector-map! + position velocity)))
;; Draw a circle 
(define (draw dc) 
  (send dc set-brush brush)
  (send dc set-pen pen)
  (send dc draw-ellipse (x position) (y position) radius radius ))
;; Initialize to random velocity, mass, and color
(x! velocity (random))
(y! velocity (random))
(set! mass (+ 1 (* 10 (random))))
(define radius (* 5 (sqrt mass)))
(define color 
  (let* ((r (random))
         (b (real->floating-point-bytes r 4)))
    (make-object color% (bytes-ref b 0) (bytes-ref b 1) (bytes-ref b 2) )))
(define brush (make-object brush% color))
(define pen (make-object pen% color))
;; Don't forget the super-new!
(super-new)
))
;; Abstract the list-handling for a list of planets
(define planet-list%
  (class object%
(public add-planet calculate-force move draw)
(init-field (planets '()))
(define (add-planet planet)
  (set! planets (cons planet planets)))
(define (calculate-force)
  (for-each (lambda (planet)
              (send planet calculate-force planets))
            planets))
(define (move)
  (for-each (lambda (planet)
              (send planet move))
            planets))
(define (draw dc)
  (for-each (lambda (planet)
              (send planet draw dc))
            planets))
(super-new)
)
  )
(define planet-list (new planet-list%))

;; The GUI
(define frame (new frame% 
               (label "Planets")
               (min-width 120)
               (min-height 80)
               ))
(send frame create-status-line)
(send frame show #t)

(define h-panel
  (new horizontal-panel%
   (parent frame)
   (stretchable-height #f)
   (style '(border))
   (border 2)))

(define run-checkbox
  (new check-box%
   (parent h-panel)
   (label "Run animation")
   (callback
    (lambda (button event)
      (cond [(send run-checkbox get-value)(thread-resume (thread-a))]
            [(not (send run-checkbox get-value)) (thread-suspend (thread-a))]
   )))
    ))

(define my-canvas%
  (class canvas%
(override on-paint on-event)

(define (on-paint)
  (let ((dc (send this get-dc))
        (w (send this get-width))
        (h (send this get-height)))
    (send dc clear)
    (send planet-list draw dc)
    ))
(define (on-event event)
  (when (send event button-down?)
    (let ((x (send event get-x))
          (y (send event get-y)))
      (send frame set-status-text (format "Mouse at ~a ~a" x y))
      (send planet-list add-planet (new planet% (position (vector x y))))

      (send this refresh)))
  )
(super-new)
(send (send this get-dc) set-background (make-object color% 8 8 64))
))

(define canvas
  (new my-canvas%
   (parent frame)
   (style '(border))
   (min-width 640)
   (min-height 480)))

;; planet animator
(define thread-a (lambda ()
(let loop ()
  (sleep/yield .1)
(send planet-list calculate-force)
(send planet-list move)
(send canvas refresh)
  (loop))))

; this creates the thread-a and starts the program

(thread-suspend (thread thread-a))

1 个答案:

答案 0 :(得分:3)

实际上,你的工作与它一样多,实际上是神奇的。

问题是thread-a不是线程。它不是产生线程的函数。这是一个永远运行的功能,移动行星并更新画布。

因此,当您的复选框的回调功能(thread-suspend (thread-a))时,thread-suspend实际上从未发生过。对thread-a的调用刚刚开始运行,永远不会返回。

GUI没有锁定的原因(如果事件回调没有返回通常会这样)是thread-a定期调用sleep/yield,这允许GUI事件循环处理更多事件。 (这就是为什么我说代码是奇迹般的。)

修复是将thread-a定义为线程本身:

(define thread-a
  (thread
    (lambda ()
      (let loop () ....))))
(thread-suspend thread-a)

并将其他引用从(thread-a)更改为仅thread-a