in reply to join() or detach() which is more memory efficient ?
If “memory efficiency” is what you are looking for, then ... don’t spawn large numbers of threads only to let them immediately die off. (What I call the “flaming-arrow algorithm.”)
Start a small pool of threads and let them be persistent. Let them service a queue into which you put work-requests, and let them either stick around forever or service some (large) number of requests before they terminate themselves. (You might do that if you are worried about memory leaks. Apache does this, for instance.)
Another good notion is to let threads handle not only the processing of requests, but their inflows and outflows as well. In this scenario, the parent thread mostly spends its time “reaping” dead children, and generally watching over things to make sure that the proper number of child threads exist, that none of them appear to be hung (or hung-over?) and so on. (And, well, sleeping most of the time.)
This arrangement is very “friendly” to the operating system, and this is where you will get the most real efficiency, both in terms of memory and in other things.
(P.S.: There are many CPAN modules out there that can save you a lot of work in setting up arrangements like this. “Do not do a thing already done.”)
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: join() or detach() which is more memory efficient ?
by BrowserUk (Patriarch) on Mar 25, 2011 at 17:02 UTC |