in reply to Dormus interruptus
A very simple solution would be to split up the long sleeps and check $quit inbetween. This could be encapsulated in something like this:
sub check_sleep { my $cnt = shift; while (($cnt-- > 0) && ($quit == 0)) { sleep 1; } }
Another possible solution (but I don't know if this works with threads) is to set up a signal handler (say for SIGUSR1) and send a signal from the main thread. I think this would interrupt the sleep() call, but I'm not sure off-hand whether the sleep would get restarted after the return from your signal handler or not.
A third possibility would be to have a shared file descriptor, and use select() with a timeout on that file descriptor instead of sleep(). You could then activate it from the main thread which would break out of the select in the other threads. I think the easiest way to get such an fd is to open a pipe, select on the read side in the worker threads and write to the writer side in the main thread
I'm sure there's more ways to do it, and I didn't give much detail on those last two ways, but it should be enough to get you started.
|
---|