Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl thread question

Reply
Thread Tools

perl thread question

 
 
cji_work@yahoo.com
Guest
Posts: n/a
 
      03-23-2005
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;
}
}

"

 
Reply With Quote
 
 
 
 
Fabian Pilkowski
Guest
Posts: n/a
 
      03-23-2005
* http://www.velocityreviews.com/forums/(E-Mail Removed) 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;
>
> 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
 
Reply With Quote
 
 
 
 
cji_work@yahoo.com
Guest
Posts: n/a
 
      03-23-2005

Fabian Pilkowski wrote:
> * (E-Mail Removed) 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;
> >
> > 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


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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-23-2005
(E-Mail Removed) wrote in
news:(E-Mail Removed) ups.com:

>
> Fabian Pilkowski wrote:
>> * (E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM
PyNew_Interpreter(): "Fatal Python error, invalid thread state for this thread" question vincent wehren Python 0 12-11-2003 08:09 PM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments