Le 17/07/2015 17:08, Matthew Flatt a écrit :
It's possible (but unlikely) for the thread bound to `a` to complete
and try to kill `b` before the variable `b` is initialized.

Ah ok, yes that is risky in this case, plus we didn't close the tcp connection.

Here's what I'd do:

  * Create a custodian to manage the thread, and shut the custodian down
    instead of killing the thread. That way, in addition to killing the
    thread, any network resources are also released.

  * Instead of starting a timer thread, use `sync/timeout`.

(define (foo)
   (define c (make-custodian))
   (define t (parameterize ([current-custodian c])
               (thread
                (λ ()
                  (let-values ([(p-in p-out)
                                (tcp-connect "smtp.gmail.com" 5887)])
                    (printf "Server port is good"))))))
   (displayln `(hello world))
   (unless (sync/timeout 2 t) ; produces #f on timeout
     (printf "Server port is not good")
     (custodian-shutdown-all c)))

That is an elegant solution :) . So I read the doc about custodian with parameterize to know more, but it is always a little mysterious for me.

when : (parameterize ([current-custodian c])
We say that we run the code in the 'body' part with a c parameter that contain an empty custodian. But we never added something to c, how the connections are 'linked' to c, when we use custodian-shutdown-all ? It is automatic for all thread/network/streams etc... functions in the 'body' part of (parameterize) ?

When we bind the (parameterize) to a variable, the type is the type of last result in the 'body' part in (parameterize) ? , So as a thread is an evt, we can use it in sync/timeout.

--
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