my $th = new Thread( \&Another::Module::run, $p );
####
D:\Perl\test>perl58 -de1
Loading DB routines from perl5db.pl version 1.19
Editor support available.
Enter h or `h h' for help, or `perldoc perldebug' for more help.
main::(-e:1): 1
DB<1> use threads
DB<2> $t=threads->new( sub{ $\ = "\nCRLF\n"; print 'test'; print 'bye'; return; } )
DB<3> test
CRLF
bye
CRLF
DB<3> print 'Hello'
DB<4> Hello
DB<4> $t->join
Scalars leaked: 1
DB<5> q
D:\Perl\test>
####
#! perl -slw
use strict;
package Another::Module;
use threads;
sub new{
my $class = shift;
return bless {@_}, $class;
}
sub run {
my $self = shift;
print $self, '|', \&Another::Module::run;
print threads->self()->tid, ' : ', join' ', %{ $self };
sleep 2;
$self->{'a new element'} = 'added in the second thread';
sleep 2;
print threads->self()->tid, ' : ', join' ', %{ $self };
}
package main;
use threads;
my $p : shared = Another::Module->new( some=>1, initialisation=>'data' );
print $p, '|', \&Another::Module::run;
print threads->self()->tid, ' : ', join' ', %{ $p };
my $t = threads->new( \&Another::Module::run, $p );
print threads->self()->tid, ' : ', join' ', %{ $p };
$p->{'a new element'} = 'added in the main thread'; # Dirty. For demo only
print threads->self()->tid, ' : ', join' ', %{ $p };
sleep 10; # Give the second thread a chance to end.
print threads->self()->tid, ' : ', join' ', %{ $p };
$t->join;
####
E:\perl58-rc3\test>test.pl3
Another::Module=HASH(0x8823dc)|CODE(0x16381cc)
0 : initialisation data some 1
0 : initialisation data some 1
0 : initialisation data a new element added in the main thread some 1
Another::Module=HASH(0x166cde4)|CODE(0x166c7f0)
1 : initialisation data some 1
1 : initialisation data a new element added in the second thread some 1
0 : initialisation data a new element added in the main thread some 1
####
bless is not supported on shared references. In the current version, bless will only bless the thread local reference and the blessing will not propagate to the other threads. This is expected to be implemented in a future version of Perl.