Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Question from the Perl Thread Tutorial

Reply
Thread Tools

Question from the Perl Thread Tutorial

 
 
grocery_stocker
Guest
Posts: n/a
 
      12-16-2006
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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      12-16-2006
"grocery_stocker" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
grocery_stocker
Guest
Posts: n/a
 
      12-16-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> "grocery_stocker" <(E-Mail Removed)> 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

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      12-16-2006
>>>>> "gs" == grocery stocker <(E-Mail Removed)> 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 ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
grocery_stocker
Guest
Posts: n/a
 
      12-16-2006

(E-Mail Removed) wrote:
> On Sat, 16 Dec 2006 00:14:53 -0500, Uri Guttman <(E-Mail Removed)> wrote:
>
> >>>>>> "gs" == grocery stocker <(E-Mail Removed)> 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
> ****? Signalas, Async/Sync???
>
> And oh, your going to Perl to find all that **** 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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      12-16-2006
"grocery_stocker" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

>
> (E-Mail Removed) wrote:

....

>> URInalysis ...


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

Sinan
 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      12-16-2006
"grocery_stocker" <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > "grocery_stocker" <(E-Mail Removed)> 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
 
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
Do programmers enjoy video tutorial or written tutorial ? Linuxmank Java 36 08-08-2009 09:54 AM
Tutorial or Example (or Tutorial) of Using Canvas to Produce a Plot W. Watson Python 13 09-20-2007 04:29 PM
Free tutorial on Object Perl (Object-Oriented Perl) tim@timothyhowe.com Perl Misc 2 12-22-2005 12:30 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM
perl 5.8.2/3 - thread started by a thread pawo Perl 0 02-16-2004 01:18 PM



Advertisments