Elijah has asked for the wisdom of the Perl Monks concerning the following question:
However if a thread returns successfully while the looping functions is still looping the thread will block essentially holding on to it's resources (mainly memory is my concern).
Now if I implement say some sort of load splitting function in the mix to try an elliviate this and make a call to join() after each callback to my load spliting function all the previous joined and returned threads still hold their resources until the entire main thread exists.
I am not sure if I am properly conveying what I want to with words so I will try to do it with code:
#!/usr/bin/perl -w use strict; use threads; my $count = 1; for (1..10) { my $thread = threads->create(\&threadsub, $count); $count++; sleep(2); } for (threads->list()) { eval{$_->join()}; print $_->tid()." has returned!\n"; } sub threadsub { my $cnt = shift; print "I am thread $cnt!\n"; sleep(5); print "returning from thread $cnt...\n"; }
Now if you run the above code you will see that the app will tell you it is returning from each thread real time but will not report a successful join and completion until the entire for loop is complete then from this point on it will report a return real time.
If I try to work around this and create a subroutine to join each thread passed to it and then make a call in the for loop after each thread creation then two things will happen that I do not want to happen. First being that the app will block waiting for a return from each thread one at a time and second even after a thread is returned the resources (memory) used by that thread will still not be released back to the OS probably due to the fact that each thread runs in the same memory space as the main thread..
Is there any way I can get around this so I can have the thread returns it's memory used to the OS after a successful return/join?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: release threads resources?
by dave_the_m (Monsignor) on Oct 11, 2005 at 09:24 UTC | |
by BrowserUk (Patriarch) on Oct 11, 2005 at 10:31 UTC | |
by Elijah (Hermit) on Oct 11, 2005 at 23:24 UTC | |
by BrowserUk (Patriarch) on Oct 12, 2005 at 02:56 UTC | |
by Elijah (Hermit) on Oct 14, 2005 at 15:07 UTC | |
by BrowserUk (Patriarch) on Oct 14, 2005 at 15:38 UTC |