You can start a second thread that monitors the condition as well as a
timer; if the timer goes off first, then you kill the first thread; if
the condition happens first, then it continues. If you don't want the
second thread to have to monitor the condition, then the first thread
should tell the second thread that everything is okay, such as by
setting a semaphore.

Like this:

```
#lang racket/base

(define (kill-unless-sema-after-secs t sema secs)
  (thread
   (λ ()
     (sync sema
           (handle-evt (alarm-evt (+ (current-inexact-milliseconds)
                                     (* 1000 secs)))
                       (λ _
                         (kill-thread t)))))))

(module+ main
  (define t
    (thread
     (thunk
       (let loop ()
         (define sema (make-semaphore))
         (kill-unless-sema-after-secs t sema secs)
         (define-values (len shost sport)
           (udp-receive! socket buffer))
         (semaphore-post sema)
         ;; xxx do stuff
         (loop))))))
```

--
Jay McCarthy
Associate Professor @ CS @ UMass Lowell
http://jeapostrophe.github.io
Vincit qui se vincit.

On Tue, Mar 24, 2020 at 4:03 PM David Storrs <david.sto...@gmail.com> wrote:
>
> I've got this code:
>
> (thread
>   (thunk
>     (let loop ()
>       (define-values (len shost sport) (udp-receive! socket buffer))
>       ...do stuff with the received message...
>      (loop))))
>
> I'd like to be able to say "If you haven't received a message in X time, kill 
> the thread".  I'm not sure how to enact that; sync/timeout won't do it since 
> the thread won't return.  I've thought of weird signaling systems using 
> channels or set! on some external value or etc, but they are all terrible.
>
> What's the right way to do this?
>
> --
> 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/CAE8gKocoTTN0VfOToioMq3xK_0ysqAQVHmZ%2BmiFS9iutzY0sxQ%40mail.gmail.com.

-- 
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/CAJYbDam%3D7teWJun3zg3c4MBtV4Ew%2Bgv6-Zj9xE7LTBew92u9RQ%40mail.gmail.com.

Reply via email to