C
cji_work
Hi all,
I am a beginner to the Perl Thread, and I need some help for the
following code. What I was trying to do is to crate two threads, one is
to write some data to the stack while the other one is to read from the
same stack ? This is just a test for me, but it does not work. Any
comments ?
Following are the code,
"
use Thread;
use Thread::Queue;
my $stream = new Thread::Queue;
my $first_thr = new Thread(\&read_number, $stream, \@stack);
my $second_thr = new Thread(\&write_number, $stream, \@stack);
$stream->enqueue(undef);
$first_thr->join();
$second_thr->join();
sub write_number {
my $ref_stack = shift;
for my $i ( 1 .. 3 ) {
for my $j ( 1 ..3) {
push(@{$ref_stack}, $i*$j);
sleep 1;
print "write::In the stack: @{$ref_stack}\n";
}
sleep 1;
}
}
sub read_number {
my $ref_stack = shift;
while(1) {
print "read::In the stack: @{$ref_stack}\n";
sleep 1;
}
}
"
I am a beginner to the Perl Thread, and I need some help for the
following code. What I was trying to do is to crate two threads, one is
to write some data to the stack while the other one is to read from the
same stack ? This is just a test for me, but it does not work. Any
comments ?
Following are the code,
"
use Thread;
use Thread::Queue;
my $stream = new Thread::Queue;
my $first_thr = new Thread(\&read_number, $stream, \@stack);
my $second_thr = new Thread(\&write_number, $stream, \@stack);
$stream->enqueue(undef);
$first_thr->join();
$second_thr->join();
sub write_number {
my $ref_stack = shift;
for my $i ( 1 .. 3 ) {
for my $j ( 1 ..3) {
push(@{$ref_stack}, $i*$j);
sleep 1;
print "write::In the stack: @{$ref_stack}\n";
}
sleep 1;
}
}
sub read_number {
my $ref_stack = shift;
while(1) {
print "read::In the stack: @{$ref_stack}\n";
sleep 1;
}
}
"