I need a thread pool. Something that would allow me to something like this.
$pool = Pool::Of::Threads->new(how_many_threads => 5); for (1 .. 1000) { $pool->job({sleep 5; print ($_: I'm in the thread}); }
As the loop runs each call to job goes to a different thread until 5 threads are used, and then the loop waits until a thread becomes available. When a thread is available the loop uses it etc...

I'd like to have the threads started in the call to new and then reused so that I don't have the cost of creating every time. I've seen Thread::Pool and it look perfect, but I can't get it to install, and when I force install scripts fail in many ways. Has anyone gotten this to work? I'd like to do any of the following:

Get Thread::Pool to work.
Find another solution that does the same.
Roll my own.

Looking into rolling my own, I can see how most of the steps will come together except one. How can I create a thread, send it a code block, get the results of that code block back, and keep the thread alive to send it another code block? Haven't found any thing like that in the docs? Does anyone have a working example of a (preferably OO) work crew style thread pool that I could use/rip off?

Thanks,
Barry

In reply to Building a thread pool by barvin

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.