worker.ss
#lang scheme/base

(define channel (make-channel))

(define (build-worker)
  (thread
   (λ ()
     (let loop ()
       (with-handlers
           ((exn:fail? (λ (e) 
                         (display (exn-message e))(newline)
                         (channel-put channel e))))
         (let ((proc (channel-get channel)))
           (proc)))
       (loop)))))

(define (build-dispatch (num-workers 4))
  (define workers (build-list num-workers (build-worker)))

  (define (replace-dead!)
    (set! workers
          (map
           (λ (worker)
             (if (thread-dead? worker)
                 (build-worker)
                 worker))
           workers)))

  (define (dispatch proc)
    (replace-dead!)
    (channel-put channel proc))
  
  dispatch)

(provide build-dispatch)