多线程中的变量共享

 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"

 

转载于:https://www.cnblogs.com/perl6/p/6462155.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值