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!


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.
RIP an inspiration; A true Folk's Guy

In reply to Re: Perl5.8.4 , threads - Killing the "async" kid by BrowserUk
in thread Perl5.8.4 , threads - Killing the "async" kid by MonkeyMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.