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:
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.
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.
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |