[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Guile fibers return values

From: Chris Vine
Subject: Re: Guile fibers return values
Date: Sun, 5 Jan 2020 12:33:29 +0000

On Sun, 5 Jan 2020 02:30:06 +0100
Zelphir Kaltstahl <address@hidden> wrote:
> This way of communication between the fiber and the main process seems
> in the style of Racket's places. Except that I can send normal
> procedures / lambdas to the fiber, which is great on a single machine,
> while I need to send serializable lambdas to Racket places (and I have
> not gotten to do that yet).
> Is there a restriction on the kind of lambdas I can send on a channel as
> I did in the example above?

I may well be missing your point, mainly because I don't know what you
mean by "the main process" - all the fibers are part of the same
process, and can be run in the same native thread if you want.

run-fibers runs what amounts to a scheduler and does not return until
the thunk passed to it returns.  So if by "the main proccess" you mean
the thunk which is running on a fiber scheduler, then you know it has
finished when run-fibers returns, after which you can execute what
other non-fiber code you want to execute.  run-fibers will return the
value (if any) returned by the thunk which it runs.

Within the thunk run by run-fibers, you normally synchronize using
channels. At it's absolute simplest it can be this:

  (display (run-fibers
            (lambda ()
              (let ((channel (make-channel)))
                 (lambda ()
                   (sleep 1) ;; do some work
                   (put-message channel "hello world")))
                (simple-format #t "~a~%" (get-message channel))

Here the "main" thunk (the one passed to run-fibers which returns
"finished\n") will not finish until the fiber thunk has finished,
because of the wait on the channel.  If you spawn multiple fibers and
the "main" thunk does not wait for the fibers like this, and you
therefore need to ensure additionally that run-fibers does not return
until all the fiber thunks have finished, you can set the drain
argument of run-fibers to #t.  Probably in that case your "main" thunk
will not return a meaningful value.

You say you want "to parallelize some algorithm".  If that is your main
aim, consider guile's parallel forms of parallel, let-par and friends.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]