Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
28
Task/Mutex/Perl/mutex.pl
Normal file
28
Task/Mutex/Perl/mutex.pl
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use Thread qw'async';
|
||||
use threads::shared;
|
||||
|
||||
my ($lock1, $lock2, $resource1, $resource2) :shared = (0) x 4;
|
||||
|
||||
sub use_resource {
|
||||
{ # curly provides lexical scope, exiting which causes lock to release
|
||||
lock $lock1;
|
||||
$resource1 --; # acquire resource
|
||||
sleep(int rand 3); # artifical delay to pretend real work
|
||||
$resource1 ++; # release resource
|
||||
print "In thread ", threads->tid(), ": ";
|
||||
print "Resource1 is $resource1\n";
|
||||
}
|
||||
{
|
||||
lock $lock2;
|
||||
$resource2 --;
|
||||
sleep(int rand 3);
|
||||
$resource2 ++;
|
||||
print "In thread ", threads->tid(), ": ";
|
||||
print "Resource2 is $resource2\n";
|
||||
}
|
||||
}
|
||||
|
||||
# create 9 threads and clean up each after they are done.
|
||||
for ( map async{ use_resource }, 1 .. 9) {
|
||||
$_->join
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue