On 5/3/17 10:41 PM, Eric Griffis wrote:
Hello,

I'm having trouble catching "terminate break" exceptions when combining 
break-thread with thread-wait.

MWE 1:

  (with-handlers ([exn:break:terminate? writeln])
    (let ([t (thread (lambda () (thread-wait (current-thread))))])
      (break-thread t 'terminate)
      (thread-wait t)))

Threads do not inherit exception handlers. You need to move the `with-handlers` to the new thread:

  (let ([t (thread (lambda ()
                     (with-handlers ([exn:break:terminate? writeln])
                       (thread-wait (current-thread)))))])
    (break-thread t 'terminate)
    (thread-wait t))

Except that isn't quite right either, because , the main thread might (very likely) send the break to `t` before `t` is ready to catch it. So we need some additional synchronization:

  (define t-ready (make-semaphore 0))
  (let ([t (thread (lambda ()
                     (with-handlers ([exn:break:terminate? writeln])
                       (semaphore-post t-ready)
                       (thread-wait (current-thread)))))])
    (semaphore-wait t-ready)
    (break-thread t 'terminate)
    (thread-wait t))

That version should reliably print out the break exception.

Ryan

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