You wish to indefinitely suspend the current thread
There are two good solutions:
(sync (make-semaphore))
(thread-wait (current-thread))
Why would you ever want to suspend the current thread? Say you are writing a server with the following architecture: the main thread does some setup, creates some worker threads to handle requests, and then has nothing to do. The main thread cannot exit, because
MzScheme will exit if it does. Hence it is necessary to suspend the current thread.
If there was only one worker thread, you could wait until it exited:
(sync/enable-break worker-thread)
Notice the call to
sync/enable-break, not
sync. Normally breaks are enabled, so this call is equivalent to just
sync, but in case they aren't it is good practice to enable them, as it allows the main thread to be interrupted by Control-C.
Two idioms you shouldn't use are:
(let loop () (loop)) (thread-suspend (current-thread))
The former is an infinite loop, which needlessly consumes CPU resources. The later will stop the main thread handling break signals. If all the worker threads exit without resuming the main thread
MzScheme will complain with "unbreakable deadlock" and immediately exit.
NB: Based on advice supplied by
MatthewFlatt? on the
PltScheme mailing list 13/06/2006.
Hmm. Why not wait on
all the threads? It seems your way, the program will never exit, unless one of the child threads actually calls "exit", which seems dangerous.
--
NoelWelsh - 13 Jun 2006