多线程中的变量共享

 1 use threads;
 2 use threads::shared;
 3 my $count:shared = 1;
 4 print "主线程中count为:$count\n";
 5 
 6 sub thread1{
 7         print "线程1增加1\n";
 8         $count++;
 9         print "在线程1中结果为:$count\n";
10         sleep(1);
11 }
12 sub thread2{
13         print "线程2增加1\n";
14         $count++;
15         print "在线程2中结果为:$count\n";
16         sleep(1);
17 }
18 
19 my $th1 = threads->new('thread1');
20 my $th2 = threads->new('thread2');
21 $th1->join();
22 $th2->join();
23 print "当所有线程结束时count的值为:$count\n"

 

posted on 2017-02-26 23:34  Perl6  阅读(1130)  评论(0编辑  收藏  举报

导航