What is the best way to create threads that will prevent a program from
exiting until they have completed their work? I'd like to be able to do
this implicitly, without cooperation from the main thread.
My first experiment uses `shift` to insert uses of `thread-wait` after the
rest of the program:
```
#lang racket
(require racket/control)
(define (thread/wait proc)
(define t (thread proc))
(shift k (k t) (thread-wait t)))
(define t1 (thread/wait (lambda () (let loop () (displayln "t1") (sleep 2)
(loop)))))
(define t2 (thread/wait (lambda () (let loop () (displayln "t2") (sleep 3)
(loop)))))
(displayln "end of main thread")
```
This doesn't work, seemingly due to an implicit `reset`/`prompt` around
each top-level definition/expression in a module.
Wrapping the entire module in a trivial `(let () _)` fixes the issue, but
this is not desirable:
```
#lang racket
(require racket/control)
(let ()
(define (thread/wait proc)
(define t (thread proc))
(shift k (k t) (thread-wait t)))
(define t1 (thread/wait (lambda () (let loop () (displayln "t1") (sleep
2) (loop)))))
(define t2 (thread/wait (lambda () (let loop () (displayln "t2") (sleep
3) (loop)))))
(displayln "end of main thread"))
```
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/e67b750a-1d9d-42fa-9712-9c03da5475a8n%40googlegroups.com.