Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
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();

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
Node Status?
node history
Node Type: perlquestion [id://238507]
Approved by broquaint
help
Chatterbox?
[stevieb]: Obviously, Travis CI just won't cut it for these distributions...
[stevieb]: I went on my merry way writing a cross-platform, network-aware system that works across Perlbrew and Berrybrew systems and runs unit tests for Perl dists on all installed versions, with the ability to manage *brew commands themselves
[stevieb]: That worked out exceptionally well, as when I started that project, I hadn't delved into hardware development yet.
[stevieb]: found a issue in MetaCPAN::Client though today for my revdep tests. At least I think it's an issue

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2017-06-25 23:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many monitors do you use while coding?















    Results (572 votes). Check out past polls.