http://qs321.pair.com?node_id=11109408


in reply to Threads inside Objects & Sharing

Hi,

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(); }

Output

... 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.