免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 998 | 回复: 0
打印 上一主题 下一主题

2个线程间的对话,有点耳熟,有点忧郁 [复制链接]

论坛徽章:
1
数据库技术版块每日发帖之星
日期:2015-07-11 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-15 10:18 |只看该作者 |倒序浏览
闲来没事看了看Perl 多线程。 写了个让2个线程聊天的代码。仅供娱乐。
chat.pl
  1. #!/usr/bin/perl


  2. use threads;
  3. use threads::shared;
  4. use Thread;
  5. use Thread::Queue;
  6. use POSIX;



  7. my %chat_ans_map = (

  8.         "Hello"        =>"Hithere",
  9.         "Who"        =>"I'm a thread.My name is ",
  10.         "old"        =>"Not as old as yesterday!",
  11.         "slaved"        =>"If you compete with a slave, you'll become one",
  12.         "lonely"        =>"Lonely is my middle name and I can't shake it off!",
  13.         "sorry"        =>"That's alright! You are a thread too, aren't you? I'm not alone!",
  14.          "hehe"        =>"hehe :D",
  15.          "Where"           =>"A place set with oak and cedar tree, somewhere between nowhere and goodbye",
  16.          "Byebye"          =>"Take care!"

  17. );

  18. my %chat_map = (

  19.         "0"        =>"Hello",
  20.         "1"        =>"Who is this speaking ?",
  21.         "2"        =>"Wow.Same here! How old are you ?",
  22.         "3"        =>"How do you like being slaved to do things incessantly?",
  23.         "4"        =>"I'm sorry to hear that!",
  24.            "5"        =>"Indeed! Are you feeling lonely out there running 24/7?",
  25.            "6"          =>"hehe",
  26.            "7"          =>"Where are you running now?",
  27.         "8"        =>"Hmmm...Byebye",
  28.         "9"          =>"END"
  29. );

  30. my $chat_ask_queue = Thread::Queue->new();
  31. my $chat_ask = threads->new(\&chat_ask,"Thread_A",Thread->self());
  32. my $chat_ans = threads->new(\&chat_ans,"Thread_B",Thread->self());

  33. $chat_ask->join();
  34. $chat_ans->join();

  35. sub chat_ask {
  36.         my ($name,$selfid) = @_;
  37.         my $index = 0;
  38.         while (1) {
  39.                 sleep 5;
  40.                 last if ($chat_map{$index} =~/END/);
  41.                 my $content = getTime()." ".$name.": ".$chat_map{$index};

  42.                 $chat_ask_queue->enqueue($content);
  43.                 $index++;
  44.                 print "$content\n";
  45.         }
  46. }


  47. sub chat_ans {
  48.         my ($name,$selfid) = @_;

  49.         while (my $feedback = $chat_ask_queue->dequeue()){
  50.                 sleep 2;
  51.                 my $answer = match_ans($feedback);
  52.                 my $content;
  53.                 $content = getTime()." ".$name.": ".$answer;
  54.                 $content = $content.$selfid if ($feedback =~/Who/);
  55.                 print "$content\n";
  56.                 last if ($feedback =~/Byebye/);
  57.         }
  58. }

  59. sub match_ans {
  60.                 my $incoming_msg = $_[0];
  61.                 foreach my $tmp (keys %chat_ans_map) {
  62.                         if($incoming_msg =~/$tmp/) {
  63.                                 return $chat_ans_map{$tmp};
  64.                         }
  65.                 }
  66.                 return $chat_ans_map{(keys %chat_ans_map)[-1]};
  67. }

  68. =cut
  69. sub exit_all {

  70.         my @thread_list = threads->list();
  71.         foreach my $tmp (@thread_list)        {
  72.                 $tmp->join();
  73.         }
  74. }
  75. =cut
  76. sub getTime {
  77.     my $timestamp=time;
  78.     my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($timestamp);

  79.     my $m = $mon +1;
  80.     my $ret = $m."-".$mday." ".$hour.":".$min.":".$sec;
  81.     return $ret;

  82. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP