Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: changing parameters in a thread
by BrowserUk (Patriarch) on Mar 29, 2009 at 12:47 UTC | |
by Anonymous Monk on Mar 29, 2009 at 12:58 UTC | |
by BrowserUk (Patriarch) on Mar 29, 2009 at 16:37 UTC | |
by jethro (Monsignor) on Mar 29, 2009 at 15:37 UTC | |
|
Re: changing parameters in a thread
by locked_user sundialsvc4 (Abbot) on Mar 29, 2009 at 22:09 UTC |