Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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();

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?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2021-10-17 01:38 GMT
Find Nodes?
    Voting Booth?
    My first memorable Perl project was:

    Results (70 votes). Check out past polls.