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

Enable strict and warnings and Perl itself will tell you that this is wrong:

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

threads->create() requires a code reference as its first argument and you are passing a reference to the return value of a method call. (Which given that the method returns a list of two items, means I cannot actually guess what it is that you are passing to create(), but I do know you ought to be seeing an error message of the form:Thread 1 terminated abnormally: Not a CODE reference)

But before we get into how you might correct the syntax and semantics issues, why are you trying to run that method in a thread? How long does it take to create this new FosDataCapture() object?

Let me tell you that your threading "design" will not (ever) work as is, and for so many reasons that it is hard to know where to start. Your understanding of threads is either so limited or wrong that I really don't know how to begin to help you. But if you answer the above question first, we might get some where


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.

Replies are listed 'Best First'.
Re^2: How do I used a threaded subroutine inside a perl object
by adamcpfeiffer (Novice) on Jul 03, 2013 at 12:27 UTC

    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

      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.