if I have a thread that never exists, what is the cleanest way to shut down the main script?

Three ways

  1. detach the never ending thread.

    When the main thread terminates, you won't get the warning.

  2. use a signal handler (to catch ^C) in the main thread to set a shared flag that the never ending thread monitors regularly, and then join the never ending thread from the signal handler before terminating.

    The disadvantage of this is that you need to ensure that and reads or attaches in your never-ending thread timeout occasionally, otherwise you will never check the shared flag until you receive something.

    That means using non-blocking IO for sockets.

  3. Faff around with per-thread signal handlers and use a signal handler in the main thread to send a kill signal to the never ending thread.

    More complicated and no real advantage over using a shared flag, as the thread handler won't respond to the kill signal if it is processing a blocking opcode like read or recv.

Which is more appropriate to your situation will depend upon how your app is currently coded?


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: Exiting a script with an 'infinitely looping' thread by BrowserUk
in thread Exiting a script with an 'infinitely looping' thread by markseger

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.