我使用Perl,以線程來處理插座信息改變Perl的哈希引用在線程
ConnectionThread 負責在哈希接收分組和解析,並且enque
QueueThread 負責處理隊列elelemts(散列條目)並更新數據庫
散列是事件並聲明爲my%Events:shared;
我傳遞的哈希參照線程,但是我注意到,每個線程得到一個差散列REF值
my $hash_ref1 = \%Events ; # referencing
print "Hash ref in main is 1 " . $hash_ref1 ."\n";
my $thr1 = threads->create(\&ConnectionThread, $hash_ref1);
my $thr2 = threads->create(\&QueueThread, $hash_ref1);
的輸出是如下
Hash ref in main is 1 HASH(0x825faa4)
Hash ref is ConnectionThread is HASH(0x8493544)
Thread started ..
Hash ref is Queue thread is HASH(0x852dd9c)
下面
是完整的代碼(說明)
use strict;
use warnings;
use Socket;
use threads;
use threads::shared;
use DBI;
my %Events:shared;
sub checkSize {
my $size;
$size =keys %Events;
print "Size of Queue in Check is ***" .$size ." \n";
}
sub QueueThread {
my ($hash_ref) = @_;
print "Hash ref is Queue thread is " . $hash_ref ." \n";
while (1==1) {
sleep (5);
}
}
sub ConnectionThread {
my ($hash_ref) = @_;
print "Hash ref is ConnectionThread is " . $hash_ref ." \n";
while (1==1) {
sleep(5);
}
}
my $hash_ref1 = \%Events;
print "Hash ref in main is 1 " . $hash_ref1 ."\n";
my $thr1 = threads->create(\&ConnectionThread, $hash_ref1);
my $thr2 = threads->create(\&QueueThread, $hash_ref1);
print "Thread started ..\n";
while (1==1) {
sleep 10;
}
這是爲了滿足你的好奇心嗎? – ikegami