Question from the Perl Thread Tutorial

G

grocery_stocker

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
 
X

xhoster

grocery_stocker said:
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
 
G

grocery_stocker

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.

...

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
 
U

Uri Guttman

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
 
G

grocery_stocker

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
 
X

xhoster

grocery_stocker said:
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
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,481
Members
44,900
Latest member
Nell636132

Latest Threads

Top