From: Robert Klemme Date: 2007-02-22T17:05:05+09:00 Subject: Re: threads within a thread On 21.02.2007 19:22, Brad Tilley wrote: > I have a program that uses threads to quickly check class B networks (65,536) > hosts for public web servers. It works great. I'd like to check for other > servers as well. Basically, I've got the hosts threaded. Now, I'd like to > thread the ports so that while the hosts are being probed concurrently that > ports on the hosts could be probed concurrently as well. It might look like > this: > I can demonstrate actually code (with only threaded hosts) if that would be > helpful, but I'd rather keep it abstract and discuss how to handle threads > within a thread... hope that makes sense. Basically there is no such thing as a thread within a thread. You can start threads from a thread - actually that's the only way since the main program is run in a thread as well. In your case I'd probably do not want to have one thread per port per host. The reason being that the overhead of a thread is not insignificant and you can generate a huge amount of threads that way. I would rather use EventMachine (as mentioned, would be a great opportunity to learn it) or a fixed number of threads like in a typical farmer worker scenario. Basically you create N threads and feed them tasks via a queue. A task would be to check one port on one host. The advantage is that you can control concurrency and find out the optimal level. Another advantage is that you save the overhead of multiple thread creations and destructions. Kind regards robert