You can create a thread and wait simultaneously on the place channel
value and also on some channel that the various workers use to check
in and see if a value is available. Here's a very simple instantiation
of this idea.

More generally, I think that one of the underappreciated parts of
Racket's built in features are `evt`s.  Do check out what they can do.
You can do much more sophisticated protocols that are tailored to
exactly what you need.

hth,
Robby

#lang racket

(define better-value-chan (make-channel))
(define checking-in-chan (make-channel))
(void
 (thread
  (λ ()
    (let loop ([better-value #f])
      (sync
       (handle-evt
        better-value-chan
        (λ (x) (loop x)))
       (handle-evt
        checking-in-chan
        (λ (c)
          (channel-put c better-value)
          (loop #f))))))))

;; number -> void
;; inform another worker about a better value
(define (found-better-value x)
  (channel-put better-value-chan x))

;; -> (or/c #f number?)
;; returns the better value, if one is available, or #f if none is
(define (check-for-better-value)
  (define c (make-channel))
  (channel-put checking-in-chan c)
  (channel-get c))

(require rackunit)
(check-equal? (check-for-better-value) #f)
(check-equal? (found-better-value 11) (void))
(check-equal? (check-for-better-value) 11)
(check-equal? (check-for-better-value) #f)


On Wed, Feb 21, 2018 at 10:50 AM, 'Paulo Matos' via Racket Users
<racket-users@googlegroups.com> wrote:
> Hi,
>
> I am trying to use places to parallelize several long running algorithms
> that can run in parallel. All of these are optimizing a function F(X1,
> ..., Xn) using different algorithms and need to communicate between
> themselves.
>
> The initial thing I thought about was having a master creating all the
> places, then each time one of the places finds a better value for
> X1,..., Xn, these are sent to the master. The master logs this and
> broadcasts the information to all the places so they can improve their
> search.
>
> This means that each of the slave places running the different algos
> need to stop what they are doing and check the broadcast channel in case
> a new value has been found. However, there seems to be no way to do an
> async place-channel-get or check if there's a value to get in a channel
> before calling place-channel-get.
>
> How could one go about doing something like this?
>
> Kind regards,
>
> --
> Paulo Matos
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to racket-users+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to