码迷,mamicode.com
首页 > 编程语言 > 详细

多线程中的变量共享

时间:2017-02-27 00:48:49      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:code   log   font   blog   new   sha   use   class   logs   

 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"

 

多线程中的变量共享

标签:code   log   font   blog   new   sha   use   class   logs   

原文地址:http://www.cnblogs.com/perl6/p/6462155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!