Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Threads inside Objects & Sharing

by marioroy (Parson)
on Nov 29, 2019 at 02:41 UTC ( #11109408=note: print w/replies, xml ) Need Help??

in reply to Threads inside Objects & Sharing


This works for me. I moved the "test" routine to the top level and receives $self and $x1. Likewise, create provides $self and $x1 for the arguments.

sub test { my ($self, $x1) = @_; ... }; sub startThread { my $self = shift; my $thr; share ($self->{x2}); print "threads->create \$self{x1}=$self->{x1}\n"; $thr = threads->create('test', $self, $self->{x1}); $thr->detach(); }


... A: closing thread $self{x2}=558 A=558 B=766 C=970 B: closing thread $self{x2}=766 A=558 B=766 C=970 C: closing thread $self{x2}=970 A=558 B=766 C=970 A=558 B=766 C=970 A=558 B=766 C=970 A=558 B=766 C=970

Regards, Mario

Replies are listed 'Best First'.
Re^2: Threads inside Objects & Sharing
by Anonymous Monk on Nov 29, 2019 at 15:09 UTC

    Thank you very much. Mario's idea is the solution for me. I just removed the x1 from the "create" call and changed the name to "__test__" to make it look more private. Passing only $self to the "test" routine is sufficient.

    The reply about returning values is interesting as well, but it won't work in my application. The code I posted here is missing the work actually being done by the "test" routine, it's a bit more than "sleep 10". ;-) The procedure will start another application that will report a ready state after some time and then continue until it gets terminated. The "waiter" will have to wait for this ready state, not the end of the thread.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11109408]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2020-09-26 21:41 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (142 votes). Check out past polls.