in reply to Re^2: multi thread problem in an simple HTTP server
in thread multi thread problem in an simple HTTP server
Okay. Let's get the easy ones out the way first.
With the typo, you aren't locking anything, so the conclusions you have drawn on the basis that you thought that you were doing locking, are now invalid.
In Perl, subroutines declared within a nested scope are:
In other words, nesting them serves no purpose other than confusion. It creates expectations in those that do not know the above that their scope is limited. It creates confusion that observers (eg. me), think that the author of the code (eg. you), may believe that their scope is limited.
The bottom line is that I don't see any convenience; just a source of confusion.
Okay. I learnt something new also :)
The problem here is this:
while ( !$stat{'done'} ) { if ( my $msg = $queue_up->dequeue() ) {
That loop spends most of its time on the second of those two lines. Waiting to dequeue() a message from the queue. But dequeue() is a blocking call, so it won't get back to the top of the loop to check $stat{done} until a message is received.
The solution is to use dequeue_nb() (nb for no block). That will ensure that the terminating condition is checked frequently. However, you will also want to add a short sleep to prevent the loop from thrashing the cpu.
while ( !$stat{'done'} ) { if ( my $msg = $queue_up->dequeue_nb() ) { ... } else { sleep 1; ## Use usleep or select for better responsiveness } }
I'll come back to this in a separate reply.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: multi thread problem in an simple HTTP server
by bravesoul (Initiate) on Apr 12, 2009 at 15:58 UTC |