|P is for Practical|
Exiting a script with an 'infinitely looping' threadby markseger (Beadle)
|on Nov 24, 2008 at 13:54 UTC||Need Help??|
markseger has asked for the
wisdom of the Perl Monks concerning the following question:
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.