I'm trying to "parallelize" my code using threading. I used 'threads' library for this matter. I'm interesting in changing variables (hash for example) w/o using the same variable in two different threads (i.e without using the threads::shared option).
For example:
my %hash_1 = (); $hash_1{"michael"} = "michael"; $hash_1{"sasi"} = "other"; $hash_1{"wife"} = 0; my $thr = threads->create(\&hello,"michael",\%hash_1)->join; my $thr1 = threads->create(\&hello,"sasi",\%hash_1)->join; print " ".$hash_1{"wife"}."\n"; # will print '0' although it was manip +ulated in sub routine 'hello' sub hello{ my ($who,$hash) = @_; print "hello from thread: $who ".$hash->{$who}."\n"; if(defined($hash->{"wife"}) && !$hash->{"wife"}){ $hash->{"wife"} = "Deena"; print " ".$hash->{"wife"}."\n"; } }
I was hoping that since I'm passing reference to hash (i.e. address) the 'hello' function will act as usual and will change the hash outside the function scope. I also tried passing parameters like this:
my $thr1 = threads->create(\&hello, qw("sasi" \%hash_1))->join;w/o to much luck, anyone have an idea how to make it work (if possible).
Thanks in advance.Michael
In reply to changing parameters in a thread by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |