in reply to shared complex scalars between threads
If you don't already have them, upgrade your threads and threads::shared modules to teh latest cpan versions. recent version have the ability to share objects:
#! perl -slw use strict; use threads; use threads::shared; use Junk; print $threads::VERSION; print $threads::shared::VERSION; sub thread { my $obj = shift; my $tid = threads->self->tid; sleep $tid; $obj->add( $tid, time() ); $obj->dump; return; } my $obj = Junk->new( abc => 123, pqr => 456 ); my $shrObj :shared = shared_clone( $obj ); my @threads = map threads->create( \&thread, $shrObj ), 1 .. 10; sleep 11; $shrObj->dump; $_->join for @threads; __END__ c:\test>junk5 1.71 1.26 bless({ # tied threads::shared::tie 1 => 1224113391, abc => 123, pqr => 456, }, "Junk") bless({ # tied threads::shared::tie 1 => 1224113391, 2 => 1224113392, abc => 123, pqr => 456, }, "Junk") ...
Junk.pm
package Junk; use Data::Dump qw[ pp ]; sub new { my $class = shift; return bless { @_ }, $class; } sub dump { my $self = shift; pp $self; } sub add{ my( $self, $key, $value ) = @_; $self->{ $key } = $value; return; } 1;
I haven't done much with this ability, and I don't know how effective it is at sharing more complex objects, but it is worth a try. From a quick scan of teh POD and inside the module I don't get how it works so I can't even make a prediction.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: shared complex scalars between threads
by gone2015 (Deacon) on Oct 16, 2008 at 00:17 UTC | |
by BrowserUk (Patriarch) on Oct 16, 2008 at 01:32 UTC | |
Re^2: shared complex scalars between threads
by Anonymous Monk on Oct 16, 2008 at 15:24 UTC | |
by BrowserUk (Patriarch) on Oct 16, 2008 at 15:53 UTC |