scsh-users
[Top] [All Lists]

Re: Threads trouble

To: Emilio Lopes <eclig@gmx.net>
Subject: Re: Threads trouble
From: Eric Knauel <eric@knauel.org>
Date: Mon, 24 Apr 2006 21:02:35 +0200
Cc: scsh-users@scsh.net
List-id: <scsh-users.list-id.scsh.net>
On Mon 24 Apr 2006 20:33, Emilio Lopes <eclig@gmx.net> writes:

> consider this program:
>
>    ;; ,open threads
>
>    (define-syntax run/strings-status
>      (syntax-rules ()
>        ((_ epf ...)
>         (call-with-values
>             (lambda ()
>               (run/port+proc epf ...))
>           (lambda (port proc)
>             (let* ((string-list (port->string-list port))
>                    (status (wait proc)))
>               (close-input-port port)
>               (values string-list status)))))))
>
>    (define my-ls
>      (lambda ()
>        (receive (out rc)
>            (run/strings-status (ls))
>          out)))
>
> [...]
>
> So far, so good.  But now try this:
>
>    > (spawn my-ls)
>    > 
>
> As you see, there is no output from `ls', as I expected.  This happens
> with 0.6.6 as well as with 0.6.7-rc1.
>
> Is this a bug or am I overseeing something obvious?

SPAWN returns immediately with an undefined return value.  Thus, you
have to pass the result of a computation using a side effect or
whatever.  In this case futures seem appropriate:

(define-record-type future :future
  (really-make-future lock var)
  future?
  (lock future-lock)
  (var future-var set-future-var!))

(define (make-fresh-future)
  (let ((lock (make-lock)))
    (obtain-lock lock)
    (really-make-future lock #f)))

(define (make-future thunk)
  (let ((f (make-fresh-future)))
    (spawn (lambda ()
              (set-future-var! f (thunk))
              (release-lock (future-lock f))))
    f))

(define (future-value future)
  (obtain-lock (future-lock future))
  (future-var future))

> (make-future my-ls)     
'#{Future}
> (future-value ##)
'("_darcs" "darcs_testing_for_nfs" "intro.ss" "intro.ss~" "rails.pdf"
"rails.ps" "rails.ss" "rails.ss~")

In general you might want to consider CML synchronous channels. 

-Eric
-- 
"Excuse me --- Di Du Du Duuuuh Di Dii --- Huh Weeeheeee" (Albert King)

<Prev in Thread] Current Thread [Next in Thread>