I've seen previous questions about how to cleanly exist a script with threads by first making sure the threads exit, but I have a different situation - at least I think I do...

My main script starts a thread that looks for socket connections/disconnections and opens/closes them accordingly and so will never exit. It shares this info with the main program via a shared variable containing the open file numbers. My test program works like a champ and when I exit the main code via a ^C it simply exists.

However, when I take the same code and include it in my real code it works fine there too but when I close that program also with a ^C, I get the infamous "A thread exited while 2 other threads were still running."

This leads to a couple of questions, the most obvious being if I have a thread that never exists, what is the cleanest way to shut down the main script? I suppose I could track down the pid of the thread and kill it from the main script but that feels like it may be masking a more serious problem. I also thought I might be able to set some shared variable in the main line that the thread sees to tell it to shut down but that doesn't feel right either. The thread is also sitting in a can_read() call and so wouldn't see the shared variable anyways.

I'm also puzzled by my simple test script didn't have this problem.

thoughts?

-mark


In reply to 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.