Forsaken has asked for the wisdom of the Perl Monks concerning the following question:

Once again I find myself in a hotspot where none other than the infinitely wise Monks could offer solace...

situation is as follows:

I have a number of objects, each of which has a filehandle running in its own little separate thread. Each of those filehandles reads in data and then calls on a coderef with that data as an argument for further processing. The problem I am faced with is that due to the coderef being called from within the thread, the sub that is called on feels the other objects do not exist. So what I'm looking for is some sort of method of safely transporting the data read in from the filehandles from the individual threads to the main thread, so as to be processed in a location where all the objects actually live, allowing me to call on other objects from within the subroutine called. I've tried to use a shared array, where lines of data would be pushed on from within the separate threads, and then be shifted out in the main thread, but that wasn't really a big success.

Any ideas, suggestions, etc? Many thanks in advance.

Replies are listed 'Best First'.
Re: threads question
by BrowserUk (Patriarch) on Jul 17, 2004 at 11:11 UTC

    The simplest solution is to use one or more instances of Thread::Queue to pass the data between your threads.

    A super search for "Thread::Queue" will turn up many examples to get you started.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      Ah, indeed, silly me. Never thought all the Thread::something modules were actually intended to work with the threads module, due to the difference in name. Looks like a whole lotta sexy code just waiting for me to put it to good use.

      Many thanks indeed.

        Yes. The namespaces are confusing.

        Unfortunately, several of the Thread::* modules intended for use with 5005threads are non-useful with ithreads, but will likely never be removed or upgraded.

        The good news is that liz generated several new modules that are. Thread::Use is small and rather uninspiringly named, but extremely Use-ful.


        Examine what is said, not who speaks.
        "Efficiency is intelligent laziness." -David Dunham
        "Think for yourself!" - Abigail
        "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon