[plt-scheme] letrec and threads

From: Dimitris Vyzovitis (vyzo at media.mit.edu)
Date: Mon Feb 19 10:30:18 EST 2007

Hi,

I am observing inconsistent behavior in letrec with
threads with a slightly convoluted scenario.

First, the simple case that works as expected:
(define (spawn thunk)
  (thread thunk))

(define (ok)
  (define (foo other ch)
    (channel-put ch other))
  (letrec ((ch1 (make-channel))
           (ch2 (make-channel))
           (foo1 (spawn (lambda () (foo foo2 ch1))))
           (foo2 (spawn (lambda () (foo foo1 ch2)))))
      (values (thread? (sync ch1)) (thread? (sync ch2)))))
> (ok)
#t
#t

And now the less simple case that results in undefined values:
(define sch (make-channel))
(define spawner
  (thread
   (lambda ()
     (let lp ((thunk (channel-get sch)))
          (thread thunk)
          (lp (channel-get sch))))))

(define (spawn thunk)
  (channel-put sch thunk))

(define (oops)
  (define (foo other ch)
    (channel-put ch other))
  (letrec ((ch1 (make-channel))
           (ch2 (make-channel))
           (foo1 (spawn (lambda () (foo foo2 ch1))))
           (foo2 (spawn (lambda () (foo foo1 ch2)))))
      (values (thread? (sync ch1)) (thread? (sync ch2)))))

> (oops)
#f
#f

This is happening consistently with 369.8 in my gnu box. Not that
letrec semantics with threads are clear, but still there is marked change in
behavior when adding the extra thread.

-- vyzo



Posted on the users mailing list.