wrote in
news: ups.com:
>
> Fabian Pilkowski wrote:
>> * schrieb:
>> >
>> > 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;
Are you sure you want to use Thread.pm? Here is why I ask:
perldoc Thread
....
For new code the use of the "Thread" module is discouraged and the
direct use of the "threads" and "threads::shared" modules is
encouraged instead.
....
> read_number(). I have tried perl(versioin 5.8.4) on both linux and
> window 2000.
>
> Any ideas ?
Given that you are doing this as a learning exercise on a recent version
of Perl, I would suggest the threads module:
#! /usr/bin/perl
use threads;
use threads::shared;
my @stack : shared;
my $read_thread = threads->create(read_number => qw());
my $write_thread = threads->create(write_number => qw());
$read_thread->join;
$write_thread->join;
sub write_number {
for my $i (1 .. 3) {
for my $j (1 .. 3) {
push(@stack, $i*$j);
sleep 1;
print "write::In the stack: @stack\n";
}
sleep 1;
}
}
sub read_number {
while(1) {
print "read::In the stack: @stack\n";
sleep 1;
}
}
__END__
D:\Home\asu1\UseNet\clpmisc> v
read::In the stack:
read::In the stack: 1
write::In the stack: 1
read::In the stack: 1 2
write::In the stack: 1 2
read::In the stack: 1 2 3
write::In the stack: 1 2 3
read::In the stack: 1 2 3
read::In the stack: 1 2 3 2
write::In the stack: 1 2 3 2
read::In the stack: 1 2 3 2 4
You'll probably need a couple of lock statements placed in the
appropriate places as well.
Sinan.