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