How would you suggest to recycle threads in practice?

In simple terms, instead of creating a new thread to process each piece of asynchronous work, you start by creating a pool of threads that sit and do nothing until your main thread passes them a piece of work to do. They then process that piece of work and instead of dieing when it is complete, they go back to waiting for another new piece of work.

Here's a simple example:

#! perl -slw use strict; use threads; use Thread::Queue; sub worker { my $Q = shift; while( my $workItem = $Q->dequeue ) { printf "[%d] Processing workiterm '%s'\n", threads->tid, $work +Item; sleep rand( 2 ); ## process $workitem } } our $WORKERS ||= 10; my $Q = new Thread::Queue; my @threads = map{ threads->create( \&worker, $Q ) } 1 .. $WORKERS; while( <> ) { ## Get workitems from stdin chomp; $Q->enqueue( $_ ); ## And queue them to the worker pool } $Q->enqueue( (undef) x $WORKERS ); ## Signal no more work $_->join for @threads; ## Wait for tehm to finish; exit; ## Done

You'd use it like this:

> ls * | perl -s tdemo.pl -WORKERS=5 [1] Processing workiterm '2of12inf.dic' [1] Processing workiterm '2of12inf.txt' [1] Processing workiterm '3' [4] Processing workiterm '345241' ...

Or like this:

>tdemo -WORKERS=3 work.dat [2] Processing workiterm '00001' [1] Processing workiterm '00002' [2] Processing workiterm '00003' [1] Processing workiterm '00004' [2] Processing workiterm '00005' [1] Processing workiterm '00006' [3] Processing workiterm '00007' [2] Processing workiterm '00008' ...

Of course, this doesn't do very much--just prints and sleeps--but this simple basic structure can be used to service a huge variety of different concurrent programming tasks. Not all, in particular, socket servers require a somewhat modified approach, but still a good proportion of concurrency tasks can be handled this way. And as you only create a limited number of threads, you gain performance because you're not constantly discarding old threads only to replace them near identical new ones.

And, the bit that is very relavent in the context of this thread, if there are any small, per-thread memory leaks, they never become an issue, because you are creating so few threads.

To be able to tailor a reply to your particular situation, you'd have to tell us what it is you are currently doing with those 10,000 threads you are creating :)


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^5: does threads (still) memleak? by BrowserUk
in thread does threads (still) memleak? by faxm0dem

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.