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 | |
by Anonymous Monk on Nov 29, 2019 at 15:09 UTC | |
by Anonymous Monk on Nov 30, 2019 at 21:21 UTC | |
Re: Threads inside Objects & Sharing
by choroba (Cardinal) on Nov 28, 2019 at 23:48 UTC | |
Re: Threads inside Objects & Sharing (return doesnt need sharing)
by Anonymous Monk on Nov 29, 2019 at 03:05 UTC |