How would one go about returning all system resources used by a thread to the OS? In a standard threaded model there will generally be some sort of looping function spawning numerous threads then a call to join after the completion of the looping function to ensure all threads run their natural course.

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?


In reply to release threads resources? by Elijah

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.