Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

references and :shared

by playingwithbots (Novice)
on Feb 25, 2003 at 17:59 UTC ( [id://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();

Replies are listed 'Best First'.
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
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found