Hi Monks,

This one feels like I am abusing Perl's OO concepts in ways they were never meant to be used, but let's see if anybody has any ideas/experience.

I have a module which has some variables that need to be shared amongst threads it spawns.

my $var : shared = 0;

Which works fine for until you declare a second instance of the module. Then it seems like the namespace of the variable is shared with all other instances of this module, and each instance is sharing the variable.

I can see the uses of this, however what I need is a way to narrow the scope of the sharing of the variable to the instance in question.

Here is some simple sample code that replicates this:

package Counter; use strict; use threads; use threads::shared; my $counter : shared = 0; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; lock $counter; $counter = shift; bless($self,$class); return $self; } sub incCounter { lock $counter; $counter++; } sub getCounter { return $counter; } 1;

and

#!/usr/bin/perl -w use Counter; my $x = new Counter( 1 ); my $y = new Counter( 10 ); print $x->getCounter()."\n"; print $y->getCounter()."\n"; print "-------------\n"; $x->incCounter(); print $x->getCounter()."\n"; print $y->getCounter()."\n";

I've been playing with sharing a hash, and then somehow generating a unique name for each instance, to guarantee independence of these variables, but that seems a bit...nasty.

So can perl do this? Can I have multiple instances of a module with shared variables that are only shared among the child threads of each instance? Again I realize Thread::Pool must be thread safe across multiple instances, so there has to be a way, I think its just a question of how nasty the solution is..


In reply to threads with shared variables across multiple instances of a module by fert

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.