之前一直没有用过DB_File,不太熟。用多线程的策略写了一段代码。所有线程结束后,打印%hash,仍然为空。请帮我看看,问题出在那?
=========================================================================
[linhy@genome2 BatchPrimer3.extension]$ less multithread.pl
#! /usr/bin/perl
use warnings;
use strict;
use Thread qw(:DEFAULT );
use warnings;
use strict;
use Thread qw(:DEFAULT );
my @task = (1..8) ; #8个任务
my (%hash1 , %hash2);
my @threadArray ;
my @threadArray ;
foreach my $thread (@task){
sleep 1;
sleep 1;
# 同时最多有3个线程
if (@threadArray >= 3){
my $oldestThread = shift @threadArray ;
$oldestThread->join() ;
}
if (@threadArray >= 3){
my $oldestThread = shift @threadArray ;
$oldestThread->join() ;
}
#新线程由此开始
my $newThread = Thread->new(\&start_thread, $thread);
push (@threadArray, $newThread);
}
my $newThread = Thread->new(\&start_thread, $thread);
push (@threadArray, $newThread);
}
#等待所有线程结束
while (my $livethread = shift @threadArray){
$livethread->join();
}
$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";
}
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;
}
print "Start Thread for $thread\n";
$hash1{$thread} = 'a'.$thread ;
$hash2{$thread} = 'b'.$thread ;
sleep 5 ;
return;
}
=======================================================================
运行显示:
[linhy@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====
[linhy@genome2 BatchPrimer3.extension]$ less 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====
[linhy@genome2 BatchPrimer3.extension]$ less multithread.pl
----- Original Message -----
From: "Yonghua (Jeff)" <yonghua.peng@gmail.com>
Sent: Wednesday, August 19, 2009 2:26 PM
Subject: [PerlChina] Re: 父子进程间变量共享
>> fork 之后各个进程的内存并不是共享的, 怎么能用这个方法呢, 呵呵。
>>
>>
>> 2009/8/19 Haiyan Lin <linhy0120@gmail.com>
>>>
>>> 能不能具体一点,在楼顶的代码中加点示例代码。谢谢。
>>>
>
>
> 其实看看模块的文档很容易搞的。
> 简单写个测试脚本仅供参考:
>
> 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 讨论组"论坛。
要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛
-~----------~----~----~----~------~----~------~--~---
没有评论:
发表评论