http://www.perlmonks.org?node_id=11109399

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

Hello, I'm having an issue with sharing data between a thread and the calling program. The following code creates three objects, and each object starts a thread that is supposed to do something and then report a result back to the main section. I am sharing $self->{x2} inside the startThread procedure and I was hoping that each instance of the object would share its own $self.

#!/usr/bin/perl #----------------------------------------------------- package p1; #----------------------------------------------------- use strict; use warnings; use threads; use threads::shared; sub new { my $class = shift; my $self = { @_ }; $self->{x2} = $self->{x1}; return bless $self, $class; } sub startThread { my $self = shift; my $thr; share ($self->{x2}); sub test { my $x1 = shift; my $r = int(rand()*1000); print "$x1: starting thread r=$r \$self{x1}=$self->{x1}\n"; sleep $r/100; print "$x1: setting variable\n"; $self->{x2} = $r; sleep 10; print "$x1: closing thread \$self{x2}=$self->{x2}\n"; threads->exit(); }; print "threads->create \$self{x1}=$self->{x1}\n"; $thr = threads->create('test', $self->{x1}); $thr->detach(); } #----------------------------------------------------- package main; #----------------------------------------------------- use strict; use warnings; my $objA = p1->new( x1 => 'A' ); my $objB = p1->new( x1 => 'B' ); my $objC = p1->new( x1 => 'C' ); print " $objA->{x1}=$objA->{x2} $objB->{x1}=$objB->{x2} $objC->{x1} +=$objC->{x2}\n"; $objA->startThread(); $objB->startThread(); $objC->startThread(); for (my $i = 1; $i < 15; $i++) { print " $objA->{x1}=$objA->{x2} $objB->{x1}=$objB->{x2} $objC->{x +1}=$objC->{x2}\n"; sleep 2; }

When I run this, I get, for example, the following output:

Variable "$self" will not stay shared at ./test1.pl line 30.
    A=A B=B C=C
threads->create $self{x1}=A
threads->create $self{x1}=B
threads->create $self{x1}=C
    A=A B=B C=C
C: starting thread r=85 $self{x1}=A
B: starting thread r=289 $self{x1}=A
C: setting variable
A: starting thread r=861 $self{x1}=A
    A=85 B=B C=C
B: setting variable
    A=289 B=B C=C
    A=289 B=B C=C
A: setting variable
    A=289 B=B C=C
C: closing thread $self{x2}=861
    A=861 B=B C=C
B: closing thread $self{x2}=861
    A=861 B=B C=C
    A=861 B=B C=C
    A=861 B=B C=C
A: closing thread $self{x2}=861
    A=861 B=B C=C
    A=861 B=B C=C
    A=861 B=B C=C
    A=861 B=B C=C
    A=861 B=B C=C

In this example, I was hoping to see "A=861 B=289 C=85" at the end. It seems that the $self belonging to the first object created (objA) is shared between all three threads and the calling program. I don't need any sharing between the threads, just separate sharing between each object instance and its calling program. How can I achieve that? Some help on this would be greatly appreciated.

Replies are listed 'Best First'.
Re: Threads inside Objects & Sharing
by marioroy (Prior) on Nov 29, 2019 at 02:41 UTC

    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

      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.

Re: Threads inside Objects & Sharing
by choroba (Cardinal) on Nov 28, 2019 at 23:48 UTC
    Named subs don't nest in Perl. That's why you're getting the warning Variable "$self" will not stay shared.

    Threads are complex and uneasy, it's better not to use them directly, but use a higher level module, e.g. Thread::Queue. In my opinion, playing with threads before one grasps basic stuff like compile time versus run time is too ambitious and doomed to fail.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Threads inside Objects & Sharing (return doesnt need sharing)
by Anonymous Monk on Nov 29, 2019 at 03:05 UTC

    Hi

    If all you're doing is returning a value, you don't need to share anything

    #!/usr/bin/perl -- use strict; use warnings; use threads stack_size => 4096; ## void context returns undef threads->create( \&worker, 'LocalA', 5 ); threads->create( \&worker, 'LocalB', 5 ); threads->create( \&worker, 'LocalC', 5 ); { ## list context returns return value my @a = threads->create( \&worker, 'LocalAlfa', 15 ); @a = threads->create( \&worker, 'LocalBravo', 15 ); @a = threads->create( \&worker, 'LocalCharlie', 15 ); } waiter(); exit( 0 ); sub worker { # require LocalMyApp; return LocalMyApp::DoTheJob(); my $obj = shift; my $sleeprand = shift; print join ' ', threads->tid, threads->self, $obj, $sleeprand, "\n"; my $rand = int(rand()*1000); sleep rand $sleeprand; return $rand; } sub waiter { while( threads->list ) { for my $joinable ( threads->list( threads::joinable ) ) { print join " ", $joinable->tid, $joinable, $joinable->join || 'undefined', ## return "\n"; } } } __END__ 1 threads=SCALAR(0xb7032c) LocalA 5 2 threads=SCALAR(0xbfc554) LocalB 5 3 threads=SCALAR(0xc84b14) LocalC 5 4 threads=SCALAR(0xd3443c) LocalAlfa 15 5 threads=SCALAR(0xdbca7c) LocalBravo 15 6 threads=SCALAR(0xe4c3f4) LocalCharlie 15 2 threads=SCALAR(0xa6d354) undefined 1 threads=SCALAR(0xa6d354) undefined 3 threads=SCALAR(0xa6d354) undefined 4 threads=SCALAR(0xa6d354) 560 6 threads=SCALAR(0xa6d354) 380 5 threads=SCALAR(0xa6d354) 552