Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by markseger (Beadle)
on Nov 24, 2008 at 15:00 UTC ( #725608=note: print w/ replies, xml ) Need Help??


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

Ahhh... I do trap the ^C in my real script but had been lazy in my test script. As soon as I put the ^C in my test script it too complained!

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

re share flag - yes, I did realize that doing this I'd need to periodically wake up and check the flag, the only real negative here is that it would delay the time it takes for my script to shut down to as much as the 'wake up ' time, something I'd prefer to avoid.

-mark


Comment on Re^2: Exiting a script with an 'infinitely looping' thread
Re^3: Exiting a script with an 'infinitely looping' thread
by BrowserUk (Pope) on Nov 24, 2008 at 15:07 UTC
    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.
      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

Re^3: Exiting a script with an 'infinitely looping' thread
by ikegami (Pope) on Nov 24, 2008 at 16:12 UTC

    A quick note: Detaching the thread doesn't cause it to exit cleanly, just silently.

    #!/usr/bin/perl use threads; DESTROY { print "Destroyed\n" } sub test { my $x = bless({}); sleep() while 1; } $SIG{INT} = sub { exit }; my $thread = threads->create('test')->detach(); print("Press Ctrl-C to exit\n"); sleep();

    If the thread exited cleanly, the above prints Destroyed. It doesn't.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://725608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-21 05:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (103 votes), past polls