多线程中的变量共享

 1 use threads;
 2 use threads::shared;
 3 my $count:shared = 1;
 4 print "主线程中count为:$count
";
 5 
 6 sub thread1{
 7         print "线程1增加1
";
 8         $count++;
 9         print "在线程1中结果为:$count
";
10         sleep(1);
11 }
12 sub thread2{
13         print "线程2增加1
";
14         $count++;
15         print "在线程2中结果为:$count
";
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
"
原文地址:https://www.cnblogs.com/perl6/p/6462155.html