in reply to Re^2: Exiting a script with an 'infinitely looping' thread
in thread Exiting a script with an 'infinitely looping' thread

e detach thread - not sure how to do this but it sounds promising.

See the pod for threads, but essentially where you currently do:

my $immortal = threads->create( ...);

doing

threads->create( ...)->detach;

is pretty much all there is to it. As the pod says:

$thr->detach()

Makes the thread unjoinable, and causes any eventual return value to be discarded. When the program exits, any detached threads that are still running are silently terminated.


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."

Replies are listed 'Best First'.
Re^4: Exiting a script with an 'infinitely looping' thread
by markseger (Beadle) on Nov 24, 2008 at 15:15 UTC
    Time for some code and here's what I'm doing. I still get the message. Gotta be some thing silly (I hope)
    #!/usr/bin/perl -w
    
    use threads;
    $SIG{"INT"}=\&sigInt;
    
    my $thread=threads->create('test')->detach();
    
    while(1) { sleep 100; }
    
    sub test { sleep 100; }
    
    sub sigInt { exit; }
    

    -mark

        Due to Perl "safe signals", sleep is an uninteruptable opcode

        The docs and my observations disagree.

        #!/usr/bin/perl $\ = "\n"; $SIG{INT} = sub { print "SIGINT" }; print time; sleep(); # Sleep forever print time;
        1227543323 SIGINT 1227543325

        Safe signals means that the handler is only called after sleep returns, but sleep returns as soon as a signal is received.

        The behaviour differs in Windows, but then again, there are no signals in Windows.

        I tried your code and it also produces the same warning. Could it be related to my version of perl? I tried both on an 5.8.0 and 5.8.5 and got the same results in both cases. I even tossed a print into the sigint handler to make sure it fired and also sleep 2 seconds before trying to exit in case I needed for the sleep in the thread to wake up. Still no luck.
        # ./foo.pl
        ^C
        A thread exited while 2 threads were running.
        

        -mark