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
#!/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