Question from the Perl Thread Tutorial

Discussion in 'Perl Misc' started by grocery_stocker, Dec 16, 2006.

  1. I have a question on the following code from the Perl Thread Tutorial.

    #!/usr/bin/perl

    use threads;
    use Thread::Queue;

    my $DataQueue = Thread::Queue->new;
    $thr = threads->new(sub {
    while ($DataElement = $DataQueue->dequeue) {
    print "Popped $DataElement off the queue\n";
    }
    });

    $DataQueue->enqueue(12);
    $DataQueue->enqueue("A", "B", "C");
    #$DataQueue->enqueue(\$thr);
    sleep(10);
    #$DataQueue->enqueue(undef);
    #$thr->join;

    When I run the program, I get the following:

    Popped 12 off the queue
    Popped A off the queue
    Popped B off the queue
    Popped C off the queue
    A thread exited while 2 threads were running.

    I was always under the impression that when you included sleep() in
    main, it would wait for the threads to clean up. Ie, you wouldn't get
    the error message "A thread exited while 2 threads were running." Why
    am I still getting this error message? Yes, I even tried putting a
    sleep in the following:

    $thr = threads->new(sub {
    while ($DataElement = $DataQueue->dequeue) {
    print "Popped $DataElement off the queue\n";
    sleep(2);
    }
    });

    And still got the same error message.

    Second question is that when I uncomment
    #$thr->join;

    And run the program, it hangs. Why?

    Chad
     
    grocery_stocker, Dec 16, 2006
    #1
    1. Advertising

  2. grocery_stocker

    Guest

    "grocery_stocker" <> wrote:
    > I have a question on the following code from the Perl Thread Tutorial.
    >
    > #!/usr/bin/perl
    >
    > use threads;
    > use Thread::Queue;
    >
    > my $DataQueue = Thread::Queue->new;
    > $thr = threads->new(sub {
    > while ($DataElement = $DataQueue->dequeue) {
    > print "Popped $DataElement off the queue\n";
    > }
    > });
    >
    > $DataQueue->enqueue(12);
    > $DataQueue->enqueue("A", "B", "C");
    > #$DataQueue->enqueue(\$thr);
    > sleep(10);
    > #$DataQueue->enqueue(undef);
    > #$thr->join;
    >
    > When I run the program, I get the following:
    >
    > Popped 12 off the queue
    > Popped A off the queue
    > Popped B off the queue
    > Popped C off the queue
    > A thread exited while 2 threads were running.
    >
    > I was always under the impression that when you included sleep() in
    > main, it would wait for the threads to clean up.


    Stop being under that impression. sleep 10 sleeps for 10 seconds, unless
    it gets woken earlier. thread clean-up has nothing to do with, unless
    something in the thread clean-up process happens to wake it up.

    ....
    >
    > Second question is that when I uncomment
    > #$thr->join;
    >
    > And run the program, it hangs. Why?


    Because the sub-thread never exits. The sub-thread is supposed to exit
    when it dequeues a false item. The main thread never enqueues this false
    item (you commented out the line that does that) so the sub-thread cannot
    dequeue it. The sub-thread is blocked waiting for the main thread to
    enqueue something and the main thread is blocked waiting for the sub-thread
    to return. Bang.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Dec 16, 2006
    #2
    1. Advertising

  3. wrote:
    > "grocery_stocker" <> wrote:
    > > I have a question on the following code from the Perl Thread Tutorial.
    > >
    > > #!/usr/bin/perl
    > >
    > > use threads;
    > > use Thread::Queue;
    > >
    > > my $DataQueue = Thread::Queue->new;
    > > $thr = threads->new(sub {
    > > while ($DataElement = $DataQueue->dequeue) {
    > > print "Popped $DataElement off the queue\n";
    > > }
    > > });
    > >
    > > $DataQueue->enqueue(12);
    > > $DataQueue->enqueue("A", "B", "C");
    > > #$DataQueue->enqueue(\$thr);
    > > sleep(10);
    > > #$DataQueue->enqueue(undef);
    > > #$thr->join;
    > >
    > > When I run the program, I get the following:
    > >
    > > Popped 12 off the queue
    > > Popped A off the queue
    > > Popped B off the queue
    > > Popped C off the queue
    > > A thread exited while 2 threads were running.
    > >
    > > I was always under the impression that when you included sleep() in
    > > main, it would wait for the threads to clean up.

    >
    > Stop being under that impression. sleep 10 sleeps for 10 seconds, unless
    > it gets woken earlier. thread clean-up has nothing to do with, unless
    > something in the thread clean-up process happens to wake it up.
    >
    > ...
    > >
    > > Second question is that when I uncomment
    > > #$thr->join;
    > >
    > > And run the program, it hangs. Why?

    >
    > Because the sub-thread never exits. The sub-thread is supposed to exit
    > when it dequeues a false item. The main thread never enqueues this false
    > item (you commented out the line that does that) so the sub-thread cannot
    > dequeue it. The sub-thread is blocked waiting for the main thread to
    > enqueue something and the main thread is blocked waiting for the sub-thread
    > to return. Bang.
    >
    > Xho
    >


    Hmm..... so in other words, it's a deadlock. I remember this discussion
    about Open2 being put into deadlock a while back. Oh gawd. I'm having
    flashbacks! I think I just need to call into K-mart and say "I can't
    show up to stock the bra section today, I have the flu." After which, I
    need to sit down and REALLY think about the whole deadlock issue.

    Chad
     
    grocery_stocker, Dec 16, 2006
    #3
  4. grocery_stocker

    Uri Guttman Guest

    >>>>> "gs" == grocery stocker <> writes:

    gs> Hmm..... so in other words, it's a deadlock. I remember this discussion
    gs> about Open2 being put into deadlock a while back. Oh gawd. I'm having
    gs> flashbacks! I think I just need to call into K-mart and say "I can't
    gs> show up to stock the bra section today, I have the flu." After which, I
    gs> need to sit down and REALLY think about the whole deadlock issue.

    you have just learned one of the several weaknesses of threads. their
    win is that everyone seems to be taught that threads solve
    everything. they just move the problems around and don't do much more
    than processes do. deadlock is always possible with any parallel system
    unless you plan around it.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Dec 16, 2006
    #4
  5. wrote:
    > On Sat, 16 Dec 2006 00:14:53 -0500, Uri Guttman <> wrote:
    >
    > >>>>>> "gs" == grocery stocker <> writes:

    > >
    > > gs> Hmm..... so in other words, it's a deadlock. I remember this discussion
    > > gs> about Open2 being put into deadlock a while back. Oh gawd. I'm having
    > > gs> flashbacks! I think I just need to call into K-mart and say "I can't
    > > gs> show up to stock the bra section today, I have the flu." After which, I
    > > gs> need to sit down and REALLY think about the whole deadlock issue.
    > >
    > >you have just learned one of the several weaknesses of threads. their
    > >win is that everyone seems to be taught that threads solve
    > >everything. they just move the problems around and don't do much more
    > >than processes do. deadlock is always possible with any parallel system
    > >unless you plan around it.
    > >
    > >uri

    >
    > URInalysis doesent know what a "thread" is to save his life !!!
    >
    > To the OP:
    >
    > There is no weakness in "threads" at all, don't let anybody tell you different.
    > Without them you would still be on a dos box.... I rest my case.
    >
    > Polling or interrupt driven? Whats better?
    > How about polling within threads? How about a thread that polls in a loop with
    > a sleep. What happens when it sleep? Yeah thats it then. What is "message driven"???
    >
    > What about synchronization? What happens when 5 threads of the same function are started?
    > Data my man, data. Who can get to it? How is that controlled?
    >
    > Mutex, Semaphore, critical section (Wondoz). Overlapped IO? What you never heard of that
    > shit? Signalas, Async/Sync???
    >
    > And oh, your going to Perl to find all that shit out huh?????
    >
    > robic0


    Dude, ease up on the crack pipe. I started to read the the Perl Thread
    Tutorial because it's 10x easier to understand the the chapters on
    Threads in "Advanced Programming in the Unix Environment" by Stevens
    and Rago. Yes, I'm aware the Perl Threads are different than Linux
    threads which are in turn different than Windows Threads. I just
    needed some kind of vague grasp/overview as to what is going on before
    I started headlong into the sections on Threads in APUE
     
    grocery_stocker, Dec 16, 2006
    #5
  6. "grocery_stocker" <> wrote in
    news::

    >
    > wrote:

    ....

    >> URInalysis ...


    robic0 is a known troll with serious mental problems. Please do not feed
    the troll.

    Sinan
     
    A. Sinan Unur, Dec 16, 2006
    #6
  7. grocery_stocker

    Guest

    "grocery_stocker" <> wrote:
    > wrote:
    > > "grocery_stocker" <> wrote:
    > > >
    > > > And run the program, it hangs. Why?

    > >
    > > Because the sub-thread never exits. The sub-thread is supposed to exit
    > > when it dequeues a false item. The main thread never enqueues this
    > > false item (you commented out the line that does that) so the
    > > sub-thread cannot dequeue it. The sub-thread is blocked waiting for
    > > the main thread to enqueue something and the main thread is blocked
    > > waiting for the sub-thread to return. Bang.
    > >
    > > Xho
    > >

    >
    > Hmm..... so in other words, it's a deadlock.


    It is a form of deadlock, I suppose, but not a classic example of deadlock.
    It is entirely unsubtle and will happen every time you run your program.
    It requires to sophisticated analysis to detect, and no freak accidents of
    timing to invoke.


    > I remember this discussion
    > about Open2 being put into deadlock a while back. Oh gawd. I'm having
    > flashbacks! I think I just need to call into K-mart and say "I can't
    > show up to stock the bra section today, I have the flu." After which, I
    > need to sit down and REALLY think about the whole deadlock issue.


    There really isn't such a thing as "the whole deadlock issue", no more than
    there is a "the traffic accident issue". It is a bunch of different issues
    that all have one small component in common.

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Dec 16, 2006
    #7
    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. pawo
    Replies:
    0
    Views:
    503
  2. Rensjuh
    Replies:
    7
    Views:
    984
    Mabden
    Sep 2, 2004
  3. W. Watson
    Replies:
    13
    Views:
    1,073
    W. Watson
    Sep 20, 2007
  4. Linuxmank
    Replies:
    36
    Views:
    1,504
    Linuxmank
    Aug 8, 2009
  5. Replies:
    2
    Views:
    178
    Brian McCauley
    Dec 22, 2005
Loading...

Share This Page