子线程中执行sleep(5),主线程也跟着sleep?
时间:2011-07-17
来源:互联网
本帖最后由 heidern 于 2011-07-17 15:20 编辑
最近在研究多线程,想写个服务器端程序.
复制代码
当threadMonitor这个线程sleep的时候,主线程也会sleep,为什么?
最近在研究多线程,想写个服务器端程序.
- #!/usr/local/perl5.12/bin/perl
- use strict;
- use warnings;
- use threads (
- 'exit' => 'threads_only',
- );
- use IO::Socket;
- use Data::Dumper;
-
- $|=1;
- my $serverFD=IO::Socket::INET->new(
- LocalPort => 12340,
- Type => SOCK_STREAM,
- Listen => SOMAXCONN,
- Reuse => 1
- ) || die $!;
-
- # create a thread to monitor the threads
- threads->create(\&threadMonitor);
-
- # when a connect accepted , create a thread to process it
- while (1) {
- threads->create(\&talk,$serverFD->accept());
- }
-
- sub talk
- {
- my ($clientFD,$clientInfo)=@_;
- my ($clientport,$clientaddr) = unpack_sockaddr_in($clientInfo);
- my $clientip = inet_ntoa($clientaddr);
- my $tid = threads->self->tid();
- print "Client From => $clientip\:$clientport My number is $tid\n";
- while (<$clientFD>) {
- print "client $tid from $clientip say: $_";
- chomp($_);
- if ($_ =~ m/hello/i) {
- print $clientFD "tid=$tid,Hello Client from $clientip\n";
- } elsif ($_ =~ m/bye/i) {
- print $clientFD "tid=$tid,Bye Bye Client from $clientip\n";
- close $clientFD;
- last;
- }
- }
- threads->self->detach();
- }
-
- sub threadMonitor
- {
- print "Monitor Thread Created!!\n";
- while(1) {
- my @ths_r=threads->list(threads::running);
- my $threadNums_running=$#ths_r+1;
- my @ths_j=threads->list(threads::joinable);
- my $threadNums_joinable=$#ths_j+1;
- print "There are $threadNums_running threads running and $threadNums_joinable threads joinable\n";
- sleep(5);
- }
- threads->self->detach();
- }
作者: heidern 发布时间: 2011-07-17
怎么就都这么迷信多线程呢。
作者: flw 发布时间: 2011-07-17
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28