in reply to Re: Howto capture array/hash back after threaded process
in thread Howto capture array/hash back after threaded process

Dear zer,
With my example of my second posting:
$VAR1 = [ bless( do{\(my $o = '168888848')}, 'threads' ) ]; A thread exited while 2 threads were running.


---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re^3: Howto capture array/hash back after threaded process
by zer (Deacon) on Mar 02, 2006 at 09:59 UTC
    #!/usr/bin/perl -w BEGIN{ use Config; die "Threadbare\n" unless $Config{'useithreads'}; } use strict; use warnings; use threads; use Data::Dumper; sub funct_array { my $self = threads->self; my ($p1,$p2,$fn) = @_; my @array_result="12345"; # Run some time consuming process return @array_result; } sub funct_hash{ my $self = threads->self; my ($p1,$p2,$fn) = @_; my %hash_result; # Run some time consuming process return \%hash_result; } my ($par1,$par2,$par3,$filename); $par1=1; $par2=2; $par3=4; $filename=3; my $array1 = new threads (\&funct_array,($par1,$par2,$par3,$filename)) +; my $hash1 = threads->new(\&funct_hash,($par1,$par2,$par3,$filename)); print "Sleep"; sleep 1; print "\nwakeup $$array1 $$hash1"; # Then I tried to see the content of the array # with Data Dumper print Dumper $array1->join; print Dumper $hash1->join;

    This returns the referances to the functions. Your functions dont return any value. This will find something with the dumper mod. This seems to be on the right track for now ill see what else i can find. There is something involving the waitpid

Re^3: Howto capture array/hash back after threaded process
by zer (Deacon) on Mar 02, 2006 at 10:30 UTC
    that means the thread id you are calling hasnt completed processing.

    The join sub is the waitpid for threads. Lets you know when it is finished processing.

      ok i was about to give up on you but then i realized the magic of referances. This should do what you need (btw why are you using dumper?):

      use warnings; use threads; use Data::Dumper; sub funct_array { my $self = threads->self; my (@array_result) = @_; # Run some time consuming process return \@array_result; } sub funct_hash{ my $self = threads->self; my ($p1,$p2,$fn) = @_; my %hash_result=($p1,$p2); # Run some time consuming process return \%hash_result; } my ($par1,$par2,$par3,$filename, @result); $par1=1; $par2=2; $par3=4; $filename=3; my $array1 = new threads (\&funct_array,($par1,$par2,$par3,$filename,@ +result)); my $hash1 = threads->new(\&funct_hash,($par1,$par2,$par3,$filename)); print "Sleep"; sleep 1; # Then I tried to see the content of the array # with Data Dumper $temp = $array1->join; print "@$temp\n"; $temp = $hash1->join; foreach (keys %$temp){ print $_."=".$$temp{$_}; }