in reply to Perl5.8.4 , threads - Killing the "async" kid
How can I ensure the main thread shuts down other threads before shutting itself down with exit.
If your only goal is to suppress the error messages, the simplest way is to use POSIX::_exit() from your main thread once it has done whatever clean-up and finalisations you require.
I don't advocate that as a general solution, but if there is no clean-up to be done in the threads, and you cannot move to a more modern version of Perl then it is the simplest.
If you are prepared to do a little more work, then use a threads::shared variable to tell your threads to finish. The complication comes because the simplicity of:
my $thread300 = async { while( sleep 300 ) { ... } };
Has to become something like:
my $timeToExit : shared = 0; sub shutdown{ print logTime." INT|ABRT|QUIT|TERM received....shutting down servic +e.\n"; $timeToExit = 1; ## Tell the threads to quit sleep 1; ## give them time to react close(LOOK_FOR_LEASE) if fileno(LOOK_FOR_LEASE); close(OUTPUT) if fileno(OUTPUT); close(ERROR) if fileno(ERROR); threads->exit(); } ... my $thread300 = async { my $sleepTime = 300; while( !$timeToExit ) { sleep 1 and next while --$sleepTime; $sleepTime = 300; ... } };
Now you can set $timeToExit = 1; in your main thread signal handler and know that the thread will terminate within one second. Not so complicated.
With later versions of threads there are the "signal handling" options, but in reality, they are no better than a simple shared variable. You still have to poll because the 'threaded-signals' (KILL etc.) don't interrupt anything.
However--and this is my recommendation--based on the pseudo-code you've posted which suggests the threads in question have no clean up to perform, nor any values to return, I would suggest upgrading to (the binary compatible) 5.8.9 and detaching the threads.
The warnings output in earlier versions about "still running detached thread" were recognised to be a design error, and were removed:
c:\test> \perl32\bin\perl -Mthreads -e"print $]; async{ sleep 1000 }-> +detach; sleep 5" 5.008009 c:\test>
What that snippet shows, is that with 5.8.9, when the main thread's 5 second sleep times out, the detached thread is still running (but sleeping) and the program ends without any warning message. Detached threads are, by definition, fire-and-forget, so the earlier warnings were just wrong.
If you really can't upgrade, using POSIX::_exit()--once you've cleaned up everything that needs cleaning up--is a hackish way of achieving your stated goal.
A final thought is: these are only informational messages. You could simple document them as being "known and correct" and otherwise just ignore them!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl5.8.4 , threads - Killing the "async" kid
by MonkeyMonk (Sexton) on Oct 12, 2010 at 11:20 UTC | |
by BrowserUk (Patriarch) on Oct 12, 2010 at 12:21 UTC | |
by MonkeyMonk (Sexton) on Oct 12, 2010 at 13:18 UTC | |
by BrowserUk (Patriarch) on Oct 12, 2010 at 13:42 UTC | |
by MonkeyMonk (Sexton) on Oct 14, 2010 at 14:52 UTC | |
| |
by MonkeyMonk (Sexton) on Oct 13, 2010 at 15:13 UTC | |
by BrowserUk (Patriarch) on Oct 13, 2010 at 15:27 UTC | |
by MonkeyMonk (Sexton) on Oct 13, 2010 at 16:06 UTC | |
|