The current Racket example for Synchronous Concurrency seems a little 
verbose and overly complicated:

https://rosettacode.org/wiki/Synchronous_concurrency#Racket

What do you think of the following example instead?

Brian Adkins

--- snip ---
(define (reader)
  (for ([line (in-lines (open-input-file "input.txt"))])
       (thread-send printer-thread line))
  (thread-send printer-thread eof)
  (printf "Number of lines: ~a\n" (thread-receive)))

(define (printer)
  (let loop ([line (thread-receive)][count 0])
    (if (eof-object? line)
        (thread-send reader-thread count)
        (begin
          (printf "~a\n" line)
          (loop (thread-receive)
                (add1 count))))))

(define printer-thread (thread printer))
(define reader-thread  (thread reader))
(thread-wait printer-thread)
--- snip ---

or the following (using a macro probably just muddies the waters, but I 
didn't like the (thread (thunk ... nesting ) :)

--- snip ---
(require syntax/parse/define)

(define-simple-macro (thread-start thread-name:id rhs:expr ...)
  (define thread-name (thread (thunk rhs ...))))

(thread-start printer-thread
       (let loop ([line (thread-receive)][count 0])
         (if (eof-object? line)
             (thread-send reader-thread count)
             (begin
               (printf "~a\n" line)
               (loop (thread-receive)
                     (add1 count))))))

(thread-start reader-thread
       (for ([line (in-lines (open-input-file "input.txt"))])
            (thread-send printer-thread line))
       (thread-send printer-thread eof)
       (printf "Number of lines: ~a\n" (thread-receive)))

(thread-wait printer-thread)
--- snip ---

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/cb2751e8-f0cb-492d-bffb-d2b0f994210a%40googlegroups.com.

Reply via email to