perl thread question

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;
}
}

"
 
F

Fabian Pilkowski

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();

You don't declare the array @stack anywhere -- and don't use it anywhere
else. Remember: $stream is the first param given to read_number(), your
reference to @stack is the second one. Your sub read_number() is using
only its first param -- just as write_number().

Btw, on my Windows XP machine (Perl 5.8.6) your script works fine when
turning on autoflush mode for STDOUT, e.g. by beginning your script with

#!/usr/bin/perl -w
use strict;
$|++; # autoflush on

Read `perldoc perlvar` to learn more about the special var »$|«.

regards,
fabian
 
C

cji_work

Fabian said:
You don't declare the array @stack anywhere -- and don't use it anywhere
else. Remember: $stream is the first param given to read_number(), your
reference to @stack is the second one. Your sub read_number() is using
only its first param -- just as write_number().

Btw, on my Windows XP machine (Perl 5.8.6) your script works fine when
turning on autoflush mode for STDOUT, e.g. by beginning your script with

#!/usr/bin/perl -w
use strict;
$|++; # autoflush on

Read `perldoc perlvar` to learn more about the special var »$|«.

regards,
fabian

Hi febian,

I have followed your suggestion to set that autoflush value and correct
the parameters. But I still get nothing from the read_number().
I have tried perl(versioin 5.8.4) on both linux and window 2000.

Any ideas ?



cji
 
A

A. Sinan Unur

(e-mail address removed) wrote in

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top