亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 4864 | 回復(fù): 9
打印 上一主題 下一主題

初學(xué)perl多線程,請(qǐng)高手指點(diǎn),為什么join后子線程還在運(yùn)行? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-09-09 10:42 |只看該作者 |倒序?yàn)g覽
小弟初學(xué)perl的多線程,邊看大駱駝邊寫(xiě)了幾行試試,發(fā)現(xiàn)調(diào)用join后子線程仍然繼續(xù)運(yùn)行?請(qǐng)高手指點(diǎn)一下!注:win32系統(tǒng)
是否對(duì)join理解有誤?還是代碼問(wèn)題?
use Thread;
print "新建線程!\n";
my $t=Thread->new(\&hello,,);
sleep(10);
print"收割線程!\n";
my $result=$t->join();
print"$result\n";
print"testing ok\n";
sub hello
    {   my $sum=0;
        while(1)
                 {
                   sleep(1);
                   print"$sum  hello,first thread program!\n";
                   $sum++;
                   if($sum>10){last;}
                 }
    }

執(zhí)行結(jié)果如下:
新建線程!
0  hello,first thread program!
1  hello,first thread program!
2  hello,first thread program!
3  hello,first thread program!
4  hello,first thread program!
5  hello,first thread program!
6  hello,first thread program!
7  hello,first thread program!
8  hello,first thread program!
收割線程!
9  hello,first thread program!
10  hello,first thread program!

testing ok

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2008-09-09 11:48 |只看該作者
join并不是殺死子線程,而是等待子線程退出。
你把子線程寫(xiě)成死循環(huán)試試就明白了。

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2008-09-09 11:54 |只看該作者
哦,還是理解有誤,謝謝!
再請(qǐng)教下,我怎么強(qiáng)制殺死子線程呢?

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2008-09-09 13:15 |只看該作者

回復(fù) #3 flywind008 的帖子

發(fā)送kill信號(hào)

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2008-09-09 21:19 |只看該作者
提示: 作者被禁止或刪除 內(nèi)容自動(dòng)屏蔽

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2008-09-10 11:44 |只看該作者

回復(fù) #1 flywind008 的帖子

你怎么發(fā)現(xiàn)調(diào)用join之后了子程序還在run?

我看輸出很正常,join之后,hello程序不是停了么?

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2008-09-10 11:49 |只看該作者
可以用 threads->exit() 退出thread?

threads->exit()

    If needed, a thread can be exited at any time by calling threads->exit(). This will cause the thread to return undef in a scalar context, or the empty list in a list context.

    When called from the main thread, this behaves the same as exit(0).

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2008-09-10 17:18 |只看該作者
如2樓大哥所說(shuō)的,我把hello改成死循環(huán)后,在主線程調(diào)用join后,信息還是一直在打印。

我改用$t1->detach();后就可以達(dá)到我要的效果了

我實(shí)際是想開(kāi)一個(gè)線程作其他操作,而主線程做監(jiān)控線程,如果發(fā)現(xiàn)子線程完成了,就進(jìn)行join操作,
若子線程運(yùn)行超時(shí),則主線程將強(qiáng)制殺掉子線程。

這樣改一下代碼就可以了
use Thread;
print "新建子線程!\n";
my $t1=Thread->new(\&hello,,);
print"等待子線程返回\n";  
my $num=0;
      while(1)
              {
                sleep(1);
                if ($t1->is_joinable()) {   
                                             print"正常收割線程!\n";
                                             $t1->join();
                                             last;
                                        }
                else {
                            $num++;
                            if($num>20){
                                               print"返回超時(shí),強(qiáng)制終止子線程運(yùn)行!\n";
                                           $t1->detach();
                                           last;
                                       }
                     }
              }
print"testing ok\n";

sub hello
    {   my $sum=0;
        while(1)
                 {
                   sleep(1);
                   print"$sum  hello,first thread program!\n";
                   $sum++;
                   if($sum>30){last; }
                 }
        return 1;
    }

但是為什么detach能夠強(qiáng)制子線程停止運(yùn)行,還是不太明白。

論壇徽章:
0
9 [報(bào)告]
發(fā)表于 2008-09-11 12:09 |只看該作者
你說(shuō)的好像不對(duì),join是等待線程中止,所以如果你的hello是個(gè)無(wú)窮循環(huán),join就應(yīng)該死等才是

另外,值得注意的是不要用Thread (deprecated),現(xiàn)在最好用threads module。

detach function是把這個(gè)線程“放棄”,程序結(jié)束的時(shí)候,這個(gè)線程會(huì)瞧瞧的中止。detach不會(huì)去主動(dòng)中止這個(gè)線程。

論壇徽章:
0
10 [報(bào)告]
發(fā)表于 2009-10-15 10:19 |只看該作者
學(xué)習(xí)了。
join會(huì)等待thread的運(yùn)行結(jié)束,detach不等待thread結(jié)束。
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專(zhuān)區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP