in reply to Re: How do I used a threaded subroutine inside a perl object
in thread How do I used a threaded subroutine inside a perl object

Hi BrowserUK, This is my first try at threads so you are right that my understanding is currently limited.

Inside the method I am trying to call, I am creating a new FosDataCapture Object. During the _init phase of this object it gathers a bunch of data via telnet from the swObject (this is a fibre channel switch). This process can take 60 seconds for switch and I will have over 30 switches when running in production. Without threading, this portion of the code will take too long to run. I think that answers the first two questions.

You stated that threads->create() requires a code reference. According to the threads link you posed it states the following:

--------- $thr = threads->create(FUNCTION, ARGS) This will create a new thread that will begin execution with the specified entry point function, and give it the ARGS list as parameters. It will return the corresponding threads object, or undef if thread creation failed. FUNCTION may either be the name of a function, an anonymous subroutine, or a code ref -------------

With this definition, my first thought my call should be like this:

$thread = threads->create($self->_openFDCObject,$swObject,$switch);

When I called it like this, $swObject and $switch do not get passed into the function. So I tried a few other structures to see what might work since I could find no examples of how to use threads inside an object when calling a function inside the object.

Any further help would be greatly appreciated. Thanks

Replies are listed 'Best First'.
Re^3: How do I used a threaded subroutine inside a perl object
by BrowserUk (Patriarch) on Jul 03, 2013 at 13:45 UTC

    You could try it this way:

    $thread = threads->create( \&_openFDCObject, $self, $swObject, $switch + );

    Which should work in as much as the thread should start and run the method, but after that I suspect things will not the way you are expecting them to.

    But frankly, it would be too hard to explain why to you, you'll need to learn by your mistakes.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
      Thanks for the information, it run threaded with the syntax you gave me. As you said, the outcome wasn't what I expect. Also, based on all of the other research I have done on threads in perl, most folks say it isn't worth using threads since they aren't lightweight. I will skip using threads and look for other ways to decrease the time it takes for the object to initialize.
        Also, based on all of the other research I have done on threads in perl, most folks say it isn't worth using threads since they aren't lightweight.

        Why do they have to be lightweight?

        If you would describe the wider picture -- eg. post the full code -- then what you want can almost certainly be solved using threads.

        But if you want to believe people who've no real knowledge of threads and do things the hard way; I wish you the very best of luck.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.