Ok.. I missed the fact that simple scalars actually keep their values when shared.. That does indeed look like a bug then, it's inconsistent.

My suggestion for the docs would be manyfold. First, they should probably mention that shared variables are implemented using 'tie'. Second, that sharing already filled complex vars (arrays, hashes) clobbers the contents. While we're at it, the Thread::Queue documentation could mention that 'elements' in its case are just simple scalars, and not references to arrays etc. (It just mentions 'scalars' which can contain any number of things).

So for threads::shared:
NOTES

sharing is implemented via tied variables, and thus all caveats applying to those should be applied here too.
BUGS
"bless" is not supported on shared references. In the current version, "bless" will bless the reference in each thread, but the objects in each will work independantly. This is expected to be implemented in a future version of Perl.
sharing hashes and arrays (and refernces to such) which already contain values is not currently supported, the contents of the hash or array will be deleted. (ie what gets shared is a new, empty structure).
structures containing code references or globs cannot be put in a shared variable.

And for Thread::Queue:
NOTE

Only simple scalars can be used in Queues, complex scalars such as references and objects cannot be used.

I've been playing around some more, and having removed code refs from my objects (and replaced with symbolic references, Muhahahaha.. ), I've gotten them shared and usable, even without using freeze/thaw. There was a bug due to a shared variable not having enough scope, which is probably also a sharing/threads bug, it was a closed variable, the functions that used it were inside the correct scope...

On another note, I have another nit to pick with shared variables (I think you mentioned this recently somewhere..), just sharing a structure (HoH in this case) isnt enough, every single value added to it needs to be shared (if briefly) as well. This is very annoying, it would be nice to be able to specify that the thing being added is being added by value, if that makes any sense. Thus:

my $HoH : shared = {}; sub add_thing { my $time = get_time(); $HoH->{'one'} = &share($time); }
wouldnt need the extra '&share' in the middle there.. silly really, as $time is about to go back out of scope.

No doubt this also makes more sense if I read the source.. I'll take a look, but I'd prefer to be able to treat such things as black boxes, and have the documentation explain what they do, without taking them apart myself.

Update: Oops, blessing propagates, but doesnt actually work.. darn it!

C.


In reply to Re: Re: ithreads, shared variables.. by castaway
in thread ithreads, shared variables.. by castaway

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.