Three of my threads just sit around waiting for a datagram to arrive from another thread using select(,,,undef). The other two have to break out of the select every x seconds to do some housekeeping, so they use select(,,,timeout).
Unfortunately, dequeue() doesn’t have a timeout feature. I don’t see any way to use dequeue_nb since during periods of inactivity, it’d have to spin in a tight loop....
Any ideas?
In general (and idealistically):Don't poll!
The really nice thing about threading is that you don't have to try and make one piece of code do more than one thing at a time. If you have two things to do -- eg. 1) wait for a message from somewhere; 2) periodically do some housekeeping; -- use two threads.
async {
while( $Q->dequeue() ) {
## process message.
}
};
async {
until( $dieNow ) {
sleep $someTime;
## do housekeeping.
}
};
In this way, you separate the concerns of the two. Each is free to get on with exactly what it needs to do at its earliest opportunity -- ie. its next time-slice --, and continue that single task to completion without ever concerning itself that it might be taking too long or the need to check what else needs to be done.
That there in a nutshell is what makes kernel threading superior to event-driven or cooperative time-sharing mechanisms. Anyone who has ever done a job that required concentration, and that they answer a phone in a timely fashion, will get this point completely. It can be done, but each task complicates, interferes with and detracts from the performance of the other.
</end-idealism>
If you were designing your application from scratch, I would definitely suggest that you remove the periodic housekeeping duties from the two 'wait-for-datagram' threads. And move them, if possible combining them, into their own thread who purpose is purely periodic housekeeping. Given that you are currently restructuring your application to use threading, I also strongly suggest that you at least consider this possibility at this point.
But, without having a good oversight of what your application is doing, or how it is currently structured and therefore what impact this type of re-structuring is likely to have, I have to realise that it may not be optimal of your time at this stage to do that.
So, you currently have something like: while( my( $nfound, $timeleft) = select( ..., $timeout ) ) {
if( $timeleft ) {
## Nothing doing, do some housekeeping
}
else {
## deal with the event
}
}
Substitute: use Time::HiRes qw[ sleep ];
...
until( $dieNow ) {
sleep( $timeout / 10 );
if( my $dgram = $Q->dequeue_nb() ) {
## Act on $dgram
}
else {
## Do housekeeping (if required).
}
}
If your original timeout is 1 second, waking up every 1/10th of a second will have negligible affect on your CPU usage whilst ensuring that you process datagrams and housekeeping in a timely fashion. Indeed, you'd have to leave this: async{ 1 while Win32::Sleep( 100 ); }->join;;
Running for a whole day to really notice that it had consumed any CPU at all.
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
|