在declare变量时加了:shared后,。 ========================= my %hash1:shared; my %hash2:shared; ========================= 好像就好了。谢谢各位了。
[li...@genome2 BatchPrimer3.extension]$ perl multithread.pl Start Thread for 1 Start Thread for 2 Start Thread for 3 Start Thread for 4 Start Thread for 5 Start Thread for 6 Start Thread for 7 Start Thread for 8 ===hash 1==== hash1: 6 a6 hash1: 3 a3 hash1: 7 a7 hash1: 2 a2 hash1: 8 a8 hash1: 1 a1 hash1: 4 a4 hash1: 5 a5 ===hash 2==== hash1: 6 b6 hash1: 3 b3 hash1: 7 b7 hash1: 2 b2 hash1: 8 b8 hash1: 1 b1 hash1: 4 b4 hash1: 5 b5 On 8月19日, 下午4时24分, "Haiyan Lin" <[email protected]> wrote: > 之前一直没有用过DB_File,不太熟。用多线程的策略写了一段代码。所有线程结束后,打印%hash,仍然为空。请帮我看看,问题出在那? > ========================================================================= > [li...@genome2 BatchPrimer3.extension]$ less multithread.pl > #! /usr/bin/perl > use warnings; > use strict; > use Thread qw(:DEFAULT ); > > my @task = (1..8) ; #8个任务 > > my (%hash1 , %hash2); > my @threadArray ; > > foreach my $thread (@task){ > sleep 1; > > # 同时最多有3个线程 > if (@threadArray >= 3){ > my $oldestThread = shift @threadArray ; > $oldestThread->join() ; > } > > #新线程由此开始 > my $newThread = Thread->new(\&start_thread, $thread); > push (@threadArray, $newThread); > > } > > #等待所有线程结束 > while (my $livethread = shift @threadArray){ > $livethread->join(); > > } > > #打印hash > print "===hash 1====\n"; > while (my ($key, $value) = each %hash1){ > print "hash1: $key\t$value\n";} > > print "===hash 2====\n"; > while (my ($key, $value) = each %hash2){ > print "hash1: $key\t$value\n"; > > } > > exit; > > sub start_thread{ > > my ( $thread) = @_ ; > print "Start Thread for $thread\n"; > $hash1{$thread} = 'a'.$thread ; > $hash2{$thread} = 'b'.$thread ; > sleep 5 ; > return;} > > ======================================================================= > > 运行显示: > > [li...@genome2 BatchPrimer3.extension]$ perl multithread.pl > Start Thread for 1 > Start Thread for 2 > Start Thread for 3 > Start Thread for 4 > Start Thread for 5 > Start Thread for 6 > Start Thread for 7 > Start Thread for 8 > ===hash 1==== > ===hash 2==== > [li...@genome2 BatchPrimer3.extension]$ less multithread.pl > > > > ----- Original Message ----- > From: "Yonghua (Jeff)" <[email protected]> > To: <[email protected]> > Sent: Wednesday, August 19, 2009 2:26 PM > Subject: [PerlChina] Re: 父子进程间变量共享 > > > 2009/8/19 liseen <[email protected]>: > >> fork 之后各个进程的内存并不是共享的, 怎么能用这个方法呢, 呵呵。 > > >> 2009/8/19 Haiyan Lin <[email protected]> > > >>> 能不能具体一点,在楼顶的代码中加点示例代码。谢谢。 > > > 其实看看模块的文档很容易搞的。 > > 简单写个测试脚本仅供参考: > > > use strict; > > use warnings; > > use DB_File; > > > if ( fork ) { # in parent > > for (1..30) { > > increase(); > > sleep 1; > > } > > > } else { # in child > > for (1..30) { > > sleep 1; > > my %hash; > > tie %hash, "DB_File", "a.db", O_RDONLY, 0444, $DB_HASH > > or die "tie dbfile failed: $!\n"; > > print $hash{'test'},"\n"; > > untie %hash; > > } > > } > > > sub increase { > > my %hash; > > > tie %hash, "DB_File", "a.db", O_RDWR|O_CREAT, 0666, $DB_HASH > > or die "tie dbfile failed: $!\n"; > > > $hash{'test'} ++; > > untie %hash; > > }- 隐藏被引用文字 - > > - 显示引用的文字 - --~--~---------~--~----~------------~-------~--~----~ 您收到此信息是由于您订阅了 Google 论坛“PerlChina Mongers 讨论组”论坛。 要在此论坛发帖,请发电子邮件到 [email protected] 要退订此论坛,请发邮件至 [email protected] 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛 -~----------~----~----~----~------~----~------~--~---
