It may or may not be obvious that using a thread from a "pool" is different from creating a new thread, in particular where not-shared variables are concerned...

Creating a new thread clones the state of the creator. That includes the state of all not-shared variables. The new thread's arguments can include references to things, even if they are not shared -- in which case the new thread receives references to its own copies of the not-shared things. You can, therefore, pass objects and other complex data structures (including file handles) to a new thread -- essentially on a pass-by-value basis (changes made by the thread do not affect the creator's copies of these things).

When passing stuff to an existing thread, using Thread::Queue as suggested by others, the simple approach is to enqueue a single scalar, which will be picked up by a worker thread to tell it what to do. That can be:

  1. a simple scalar, into which the dispatcher can pack whatever it wants. To pass file/socket handles the trick is to pass the file number. When unpacking the data the worker thread can rebuild and rebless objects; for file handles it can dup the file number.

  2. a reference to a shared variable (scalar, array or hash), in which case the reference is passed directly -- but it's worth checking whether a given object can be shared ! See "BUGS AND LIMITATIONS" in threads::shared.

  3. until relatively recently the above were the only options. With recent Thread::Queue, however, you can enqueue a reference to a not-shared variable, in which case it will be cloned (recursively) into a shared variable and a reference to the shared version is passed.

    The last change relevant to this facility appears to be v2.08 14-May-2008. Where objects are concerned this is subject to the same "BUGS AND LIMITATIONS".

    This means that you can prepare an argument list and pass it to the worker thread (by enqueuing a reference to that list) in much the same way as creating a new thread and passing arguments to it. Which is something of a step forward, IMHO -- though I have not tested it to destruction.


In reply to Re: How to create thread pool of ithreads by gone2015
in thread How to create thread pool of ithreads by elf_firein

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.