Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
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 lurking in the Monastery: (11)
As of 2015-07-06 15:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls