perl中的lock

 1 #!/usr/bin/env perl -w
 2 use strict;
 3 use threads;
 4 use threads::shared;
 5 
 6 my $count:shared = 1;
 7 print "count的起始值为:$count\n";
 8 sub th_lock{
 9         $count++;
10         print "已把变量增加1并锁定10秒\n";
11         lock($count);
12         sleep(10);
13 }
14 sub th_wait{
15         print "准备量锁定count\n";
16         lock($count);
17         print "变量锁定成功\n";
18 }
19 my $th_lock = threads->new(\&th_lock);
20 my $th_wait = threads->new('th_wait');
21 $th_lock->join();
22 $th_wait->join;
23 print "count的最后值为:$count\n";

 

posted on 2017-02-27 00:03  Perl6  阅读(400)  评论(0编辑  收藏  举报

导航