From: Robert Klemme Date: 2006-02-24T18:33:32+09:00 Subject: Re: Two Ruby threading questions codeslinger wrote: > Robert Klemme wrote: >> codeslinger wrote: >>> 1. Is there a commensurate to Java's setDaemon() functionality for >>> Ruby? I've looked around on the Web, but I can't find reference to >>> anything and there's nothing in the docs about it. For those not >>> familiar with Java, setDaemon() is equivalent to setting a thread >>> to a "detached" state in pthread lingo. >> >> No. You will have to terminate them manually on exit. > > If this is true (see my last post), then this is a design flaw in > Ruby's threading library. This particular application will be > long-running and cannot afford to have dead threads laying around > waiting for exit (which hopefully will never come). I understand that > I can join the threads in the main loop periodically to avoid this > fate, but that is still hackish. Perhaps a Thread#join_on_exit= or > Thread#autojoin= or Thread#detach= method would fit the bill for this? Joining won't help because essentially this prevents the process from terminating. I was wrong though (see Joel's posting). Pickaxe II states in chapter 11 (page 137) that all threads are killed when the main thread exits. That's why threaded programs usually end with a loop that joins over all threads. Somehow this joining became second nature that I completely overlooked that here. So to put things straight, in Java lingo all Ruby threads but the main thread are daemon threads. > I got that. Thanks for all the help, Robert. You're welcome - although I mislead you on the nature of threads. I'm sorry for that. The usual idiom I follow is to create all threads that must do their work, signal them for termination depending on the nature of the processing done (e.g. send a terminator message through a queue for a producer consumer scenario) and have main thread join all threads before exiting. What kind of application are you building? Kind regards robert