koti688 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use threads; use threads::shared; my $foo : shared; $thr = threads->new(\&sub1, "THREAD1 :"); $thr2 = threads->new(\&sub2, "THREAD2 :"); @ReturnData = $thr->join; print @ReturnData; @ReturnData = $thr2->join; print @ReturnData; sub sub1{ my $i; { lock $foo; $foo=0; } for ($i=0;$i<10000;$i++) { print $_[0].$i."\n"; } { lock $foo; $foo=1; } print " Final value :$foo\n"; } sub sub2{ my $i; for ($i=0;$i<5000;$i++) { print $_[0].$i."\n"; } $out = 1; while ($out) { { lock $foo; if ($foo == 1) { $out = 0; } } } for ($i=5000;$i<10000;$i++) { print $_[0].$i."\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help on Understanding Locks in Multithreading
by zentara (Cardinal) on Feb 25, 2009 at 11:38 UTC | |
|
Re: Help on Understanding Locks in Multithreading
by Anonymous Monk on Feb 25, 2009 at 10:55 UTC | |
by koti688 (Sexton) on Feb 25, 2009 at 11:12 UTC | |
by gone2015 (Deacon) on Feb 25, 2009 at 13:04 UTC | |
by Wiggins (Hermit) on Feb 25, 2009 at 20:38 UTC | |
by Anonymous Monk on Feb 25, 2009 at 12:18 UTC | |
by Anonymous Monk on Feb 25, 2009 at 12:20 UTC |