CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced: Re: [KingNothing] Release memory from threads: Edit Log



budman
User

Mar 4, 2012, 9:05 AM


Views: 11616
Re: [KingNothing] Release memory from threads

The error means threads weren't joined or detached.

I think I see what you are trying to accomplish using detach.


Code
threads->yield();  
my @list = threads->list();
printf ("Thread %d running %d\n", $_->tid, $_->is_running) for @list;
print "$bar\nContinuing\n$bar\n";
while ( @list = threads->list() ) {
foreach my $t (@list) {
if ( ! $t->is_running() ) {
printf "Thread %d is no longer running\n", $t->tid;
$t->detach;
if ( $t->is_detached ) { print "\tdetached\n" }
else { warn "Error: could not detach\n" }
my (@remaining) = threads->list(threads::running);
print "\t",scalar(@remaining)," threads remain\n";
}
}
# do whatever else
sleep(1);
}

Output:

========================================
Submit processes
========================================
Thread 1 running 1
Thread 2 running 1
Thread 3 running 1
Thread 4 running 1
Thread 5 running 1
========================================
Continuing
========================================
Thread 5 is no longer running
detached
4 threads remain
Thread 3 is no longer running
detached
3 threads remain
Thread 4 is no longer running
detached
2 threads remain
Thread 1 is no longer running
detached
1 threads remain
Thread 2 is no longer running
detached
0 threads remain
========================================


Let me clarify a bit more about join and detach, I never really used detach, my understanding was it let the processes complete when done, that was incorrect, any processes that are detached will be killed silently when parent quits.

Joinable means you want to capture data from the threads. You use is_joinable to allow you to do other things, while the thread is running. If you just issued the join without checking, the loop would wait (block) until the thread completed.


Code
threads->yield(); # tells OS to let threads have cpu time 
while ( my @list = threads->list ) {
foreach my $t (threads->list(threads::joinable) ) {
my @result = $t->join;
# do whatever to the results from the thread
my (@remaining) = threads->list(threads::running);
print "\t",scalar(@remaining)," threads remain\n";
}
# do other stuff
sleep(1);
}



Detach means you don't care about the output of the thread, you are done with it. Once detached, you cannot rejoin it. You monitor it by checking is_running and if detach was successful with is_detached.

Check out this tutorial on threads
http://perldoc.perl.org/threads.html

You may want to look into Fork::Manager on CPAN, that does a lot of the work in the background.


(This post was edited by budman on Mar 4, 2012, 9:45 AM)


Edit Log:
Post edited by budman (User) on Mar 4, 2012, 9:37 AM
Post edited by budman (User) on Mar 4, 2012, 9:39 AM
Post edited by budman (User) on Mar 4, 2012, 9:41 AM
Post edited by budman (User) on Mar 4, 2012, 9:45 AM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives