免费注册 查看新帖 |

Chinaunix

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

子线程中执行sleep(5),主线程也跟着sleep? [复制链接]

论坛徽章:
0
发表于 2011-07-17 15:19 |显示全部楼层
本帖最后由 heidern 于 2011-07-17 22:28 编辑

最近在研究多线程,想写个服务器端程序.
  1. #!/usr/local/perl5.12/bin/perl
  2. use strict;
  3. use warnings;
  4. use threads (
  5.         'exit' => 'threads_only',
  6. );
  7. use IO::Socket;
  8. use Data::Dumper;

  9. $|=1;
  10. my $serverFD=IO::Socket::INET->new(
  11.         LocalPort       =>      12340,
  12.         Type            =>      SOCK_STREAM,
  13.         Listen          =>      SOMAXCONN,
  14.         Reuse           =>      1
  15. ) || die $!;

  16. # create a thread to monitor the threads
  17. threads->create(\&threadMonitor);

  18. # when a connect accepted , create a thread to process it
  19. while (1) {
  20.         threads->create(\&talk,$serverFD->accept());
  21. }

  22. sub talk
  23. {
  24.         my ($clientFD,$clientInfo)=@_;
  25.         my ($clientport,$clientaddr) = unpack_sockaddr_in($clientInfo);
  26.         my $clientip = inet_ntoa($clientaddr);
  27.         my $tid = threads->self->tid();
  28.         print "Client From => $clientip\:$clientport My number is $tid\n";
  29.         while (<$clientFD>) {
  30.                 print "client $tid from $clientip say: $_";
  31.                 chomp($_);
  32.                 if ($_ =~ m/hello/i) {
  33.                         print $clientFD "tid=$tid,Hello Client from $clientip\n";
  34.                 } elsif ($_ =~ m/bye/i) {
  35.                         print $clientFD "tid=$tid,Bye Bye Client from $clientip\n";
  36.                         close $clientFD;
  37.                         last;
  38.                 }
  39.         }
  40.         threads->self->detach();
  41. }

  42. sub threadMonitor
  43. {
  44.         print "Monitor Thread Created!!\n";
  45.         while(1) {
  46.                 my @ths_r=threads->list(threads::running);
  47.                 my $threadNums_running=$#ths_r+1;
  48.                 my @ths_j=threads->list(threads::joinable);
  49.                 my $threadNums_joinable=$#ths_j+1;
  50.                 print "There are $threadNums_running threads running and $threadNums_joinable threads joinable\n";
  51.                 sleep(5);
  52.         }
  53.         threads->self->detach();
  54. }

复制代码
当threadMonitor这个线程sleep的时候,主线程也会sleep,为什么?

另:我的测试方法是,先运行这个脚本,然后开另外一个终端执行下面的命令:
while(true);do echo "hello" |nc localhost 12340;done
刚开始执行的时候还不错,但server端执行到sleep那的时候客户端就会出现被阻塞的情况,5秒后就继续了

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
发表于 2011-07-17 18:20 |显示全部楼层
怎么就都这么迷信多线程呢。

论坛徽章:
0
发表于 2011-07-17 20:54 |显示全部楼层
一开始就该detach吧,要不主线程会等待子线程完成之后才继续。 (我也不是很清楚,你自己试试)

论坛徽章:
0
发表于 2011-07-17 20:54 |显示全部楼层
一开始就该detach吧,要不主线程会等待子线程完成之后才继续。 (我也不是很清楚,你自己试试)

论坛徽章:
0
发表于 2011-07-17 21:02 |显示全部楼层
试了,没用..

论坛徽章:
0
发表于 2011-07-17 22:10 |显示全部楼层
我也装了个perl试了下, 没发现主线程被阻塞。我把sleep调到10了, 新客户端连过来的时候立刻就有反应。
windows 平台下

论坛徽章:
0
发表于 2011-07-17 22:30 |显示全部楼层
我也装了个perl试了下, 没发现主线程被阻塞。我把sleep调到10了, 新客户端连过来的时候立刻就有反应。
...
cyril851122 发表于 2011-07-17 22:10



    我的系统是centos 5.6 64位的,
    我在原帖补充了我的测试方法.

论坛徽章:
0
发表于 2012-02-20 21:21 |显示全部楼层
回复 1# heidern

测试程序有问题。
    实际你只启动了一个 nc localhost 12340 进程

试试下边的
while :; do echo -e 'hello' | nc localhost 12340&; done
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP