Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

thread on method

by lepetitalbert (Abbot)
on Nov 24, 2009 at 04:38 UTC ( [id://808996]=perlquestion: print w/replies, xml ) Need Help??

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

Hello dear Monks

I'm trying to convert the following :

#!/usr/bin/perl use strict; use warnings; use threads; sub func { print "doing job ... \n"; sleep 1; } my @threads; foreach ( 1..2 ) { print "launching job\n"; push( @threads , threads->new( \&func )); } foreach my $thread ( threads->list() ) { my @command_output = $thread->join ; print "job done\n"; }

to something like

package myTest; sub new { my $item={}; bless $item; return $item; } sub myTestFunc { my $item = shift; print "doing job ... \n"; } 1;
#!/usr/bin/perl use strict; use warnings; use threads; use myTest; my @threads; foreach ( 1..2 ) { print "launching job\n"; my $test = myTest->new(); push( @threads , threads->new( $test->myTestFunc )); } foreach my $thread ( @threads ) { my @command_output = $thread->join ; print "job done\n"; }

this produces

Thread 1 terminated abnormally: Undefined subroutine &main::1

caused by $test->myTestFunc in threads->new( $test->myTestFunc ))

I understand that threads->new() is excepting a function ref (right ?)

But this kind of stuff must be possible !?

Thanks and have a nice day.


"There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

Replies are listed 'Best First'.
Re: thread on method
by BrowserUk (Patriarch) on Nov 24, 2009 at 05:07 UTC

    You can't derive a coderef to a method because it wouldn't have its invocant. So pass a subref that invokes the method:

    #!/usr/bin/perl use strict; use warnings; use threads; use myTest; my @threads; foreach ( 1..2 ) { print "launching job\n"; my $test = myTest->new(); push( @threads , threads->new( sub{ $test->myTestFunc } )); } foreach my $thread ( @threads ) { my @command_output = $thread->join ; print "job done\n"; } __END__ c:\test>t-junk2.pl launching job launching job doing job ... doing job ... job done job done

    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.

      Hi BrowserUk,

      Thanks a lot !

      Have a nice day.


      "There is only one good, namely knowledge, and only one evil, namely ignorance." Socrates

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://808996]
Approved by redgreen
Front-paged by redgreen
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-25 12:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found