I have a server with a main dispatcher loop that receives messages and
farms them out to processing threads.  I'd like to have able to have
later messages inform the behavior of earlier message processing -- an
example would be "message 1: here is a file to download" followed by
"stop downloading if you're still doing it".

I'll do the simple version, where it keeps the channels around until
the thread terminates but doesn't actually use the channels:

; main dispatcher loop in the server.  Receives messages, dispatches
to child thread for processing
 (struct file-info (hash) #:transparent)
 (define channels (make-weak-hash))
 (let loop ()
    (define msg (get-next-message)) ; e.g. (file-info "sha1-hash-of-file-X")
    (hash-set! channels (file-info-hash msg) ch)
    (thread (thunk (process-file msg ch)))))
    (loop))

1) Am I correct that this does not work? The string that came out of
file-info-hash is not eq? to the one stored inside msg, meaning that
it's not referenced from anywhere else, so it's going to disappear the
moment it goes into the weak hash.


2) The following also would not work:
(let loop ()
    (define msg (get-next-message))
    (define the-str (file-info-hash msg))  ;; save the value out before using it
    (hash-set! channels the-str ch)
    (thread (thunk (process-file msg ch)))))
    (loop))

The (loop) is in tail position, so this is not a recursive call.  That
means the stack frame is cleared, so 'the-str' is not reachable and
it's cleared from the weak hash.


2) This WOULD work:
(let loop ()
    (define msg (get-next-message)) ; e.g. (file-info "sha1-hash-of-file-X")
    (hash-set! channels msg ch)
    (thread (thunk (process-file msg ch)))))
    (loop))

In this case, 'msg' is still reachable from the processing thread, so
it remains in the weak hash until the processing thread terminates, at
which point it is removed from the weak hash and the corresponding
channel is GC'd.

-- 
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/CAE8gKodx-FB6MJKqMs8jNse4bHivKCihR6saWsi14u3b%2BBzy7A%40mail.gmail.com.

Reply via email to