in reply to Re^4: Avoid Locking Entire Hashes
in thread Avoid Locking Entire Hashes
Try it this way:
#! perl -slw use strict; use threads; use threads::shared; our %h : shared; # Setting up just a single row to increase chances # a race condition our $k = 'AAA'; our $val : shared = 0; our $THREADS = 50; our $iter = 50; $h{$k} = \$val; # Safe_set similar to BrowserUk sub safe_set { # Critical Section { lock ${$h{$k}}; ${ $h{$k} } = ${ $h{$k} } + 1; } } # Keep locking to increment $$h{$k} sub test_safe_set { for (my $i = 0; $i < $iter; ++$i) { safe_set(); } } my @pool = map{ threads->create(\&test_safe_set) } 1 .. $THREADS; $_->join for @pool; warn ${$h{$k}}, "failed\n" if ${$h{$k}} != $THREADS * $iter;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Avoid Locking Entire Hashes
by jagan_1234 (Sexton) on Jun 15, 2011 at 20:35 UTC | |
by BrowserUk (Patriarch) on Jun 15, 2011 at 21:45 UTC | |
by jagan_1234 (Sexton) on Jun 15, 2011 at 22:04 UTC | |
by BrowserUk (Patriarch) on Jun 15, 2011 at 22:25 UTC |