Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: bug in threads::shared or is it just me?

by zentara (Archbishop)
on Apr 05, 2012 at 16:46 UTC ( [id://963712]=note: print w/replies, xml ) Need Help??


in reply to bug in threads::shared or is it just me?

is this a bug, or the intended behaviour?

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

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

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

    uhm, I don't agree on the necessary behaviour part, and I think you are mixing things up.

    first of all, I'm returning the value of a variable, and this should work with any Perl variable, and it has nothing to do with scope. example:

    sub foo { my $scoped_var = 42; return $scoped_var; } say foo();

    of course the value "42" does not go out of scope. it would be very unkind if it did :-)

    second, the sub that defines the variable is running in the same thread as main::. the anonymous sub I pass to threads->create() does run in another thread, and that's exactly why the variable is shared. but I'm not switching threads between the declaration and the usage outside of the sub.

    I know that our works, as defining the variable outside of the sub does, but why? there is no mention in the threads::shared documentation about the fact that shared variables must be declared globally. furthermore, if you do the same thing with a scalar variable (eg. define it shared in a sub and returns it to the caller) everything works.

    also, if you read my first post carefully, you will see that the keys of the hash (which are created in another thread as well) are effectively returned. just the values not. so it seems to me that there is (or better said there was, according to dave_the_m's reply below) some bug with container variables, threads::shared and scoping.

    cheers,
    Aldo

    King of Laziness, Wizard of Impatience, Lord of Hubris

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://963712]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-16 21:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found