Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

references and :shared

by playingwithbots (Novice)
on Feb 25, 2003 at 17:59 UTC ( #238507=perlquestion: print w/ replies, xml ) Need Help??
playingwithbots has asked for the wisdom of the Perl Monks concerning the following question:

I want to do the following:

my $ref:shared = {};

because I want to share a hash that I'm using like:

$ref->{a}[0] = something; $ref->{a}[1] = somethingelse;

and need to share the data structure between my main routine and a thread.

But I get errors saying I can't make the ref shared. Anyone know how to "share" a reference. I also get an error when I try to:

my $ref:shared = someobj::new();

Comment on references and :shared
Select or Download Code
Re: references and :shared
by jasonk (Parson) on Feb 25, 2003 at 18:18 UTC

    You can share references, just not in quite the same way. Instead of my $ref : shared = {};, use my $ref = &share({});. To share a multidimensional object you will also need to share the anonymous refs in your code the same way, so for your second example you will have to do:

    use threads::shared; my $ref = &share({}); $ref->{a} = &share([]); $ref->{a}[0] = something;

    For shared objects you can use the same technique: my $ref = &share(someobj::new()). (Note that this is covered in the threads::shared documentation).

      Thanks, that did the trick. I guess I read the ":shared" stuff and used it and never went back to check the docs for a solution.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://238507]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-07-14 07:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (255 votes), past polls