免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2267 | 回复: 0

多线程copy [复制链接]

论坛徽章:
0
发表于 2010-08-04 14:12 |显示全部楼层
本帖最后由 bintre 于 2010-08-04 14:43 编辑
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use threads;
  5. use threads::shared;
  6. use Thread::Queue;
  7. use File::Find;
  8. use Benchmark;
  9. use File::Copy;
  10. my ($time_start,$time_end);
  11. $time_start=new Benchmark;
  12. my $infile = new Thread::Queue;
  13. my $MAX_THREADS = 10;
  14. for (my $n = 0; $n < $MAX_THREADS; $n++)
  15. {
  16.         threads->create(\&thread_copy);
  17. }

  18. find(\&wanted,"./");
  19. sub wanted {
  20.         my $file;
  21.         $file=$File::Find::name;
  22.         $infile->enqueue($file);
  23.         if($infile->pending() > $MAX_THREADS * 5){
  24.                 sleep 1;
  25.                
  26.         }
  27.        
  28. }

  29. while($infile->pending() > 0){
  30.         sleep 1;
  31. }

  32. $time_end=new Benchmark;
  33. my $usetm=timediff($time_end, $time_start);
  34. print "use time: ", timestr($usetm), "\n";
  35. print "Press Enter to exit ...";
  36. <STDIN>;

  37. foreach my $thread (threads->list(threads::all))
  38. {       
  39.         $thread->detach();
  40. }

  41. sub thread_copy{
  42.         while( my $file=$infile->dequeue()){
  43.         print "$file\n";
  44.         File::Copy::copy($file,"~/tmp");
  45.         }
  46. }
复制代码
文件太多了。整个多线程的。各位看看有缺陷吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP