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.


In reply to Threads inside Objects & Sharing by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.