Hi all,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.