in reply to bug in threads::shared or is it just me?
I think its neccessary behavior. If the shared variable declaration is inside a scoped sub, how can the main:: thread keep it shared in it's global scope? As soon as the sub finishes, the shared var is out of scope. I always have seen shared vars declared in the global section of main::.
You might be confusing the behavior of shared with our?
Look at this code where our is used instead of my. It works.
#!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; use Data::Dumper; sub test { # my %h ; shared; #dosn't work our %h : shared; #works for (1 .. 3) { threads->create(sub { my $n = shift; lock %h; $h{$n} = rand; }, $_)->join; } return %h; } print Dumper {test()};
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: bug in threads::shared or is it just me?
by dada (Chaplain) on Apr 10, 2012 at 15:36 UTC |