perl thread question

Discussion in 'Perl Misc' started by cji_work@yahoo.com, Mar 23, 2005.

  1. Guest

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

    "
    , Mar 23, 2005
    #1
    1. Advertising

  2. * 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
    Fabian Pilkowski, Mar 23, 2005
    #2
    1. Advertising

  3. Guest

    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;
    > >
    > > 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
    , Mar 23, 2005
    #3
  4. wrote in
    news::

    >
    > 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.
    A. Sinan Unur, Mar 23, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,767
  2. pawo
    Replies:
    0
    Views:
    471
  3. vincent wehren
    Replies:
    0
    Views:
    418
    vincent wehren
    Dec 11, 2003
  4. PerlFAQ Server

    FAQ 1.4 What are Perl 4, Perl 5, or Perl 6?

    PerlFAQ Server, Jan 23, 2011, in forum: Perl Misc
    Replies:
    0
    Views:
    286
    PerlFAQ Server
    Jan 23, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    652
    PerlFAQ Server
    Feb 3, 2011
Loading...

Share This Page