Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

How can perl thread exit elegantly

by anaconda_wly (Scribe)
on Apr 22, 2013 at 10:10 UTC ( #1029837=perlquestion: print w/ replies, xml ) Need Help??
anaconda_wly has asked for the wisdom of the Perl Monks concerning the following question:

I create a lot of threads, and I'm using

$SIG{INT} = \&catch_CtrlC;

to handle ctrl+c exit.

I want to issue an event from catch_CtrlC to every running threads to exit elegantly(release resources before exit). How to realise this in the threads?

I mean: How to inform the thread that a Ctrl+C signal has been triggered and needs to prepare to exit?

Comment on How can perl thread exit elegantly
Download Code
Re: How can perl thread exit elegantly
by Anonymous Monk on Apr 22, 2013 at 10:30 UTC
Re: How can perl thread exit elegantly
by BrowserUk (Pope) on Apr 22, 2013 at 11:51 UTC
    I want to issue an event from catch_CtrlC to every running threads to exit elegantly(release resources before exit). How to realise this in the threads?

    It depends what those threads are doing and what resources they need to release?

    There are several approaches to the problem, and which is appropriate is dependent upon the code in question, so post your code.

    The one approach I cannot recommend at all, is using the Threads signalling mechanism which is entirely redundant.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
      Thanks! Can we define signal of our own?
        Can we define signal of our own?

        No. Signals and threading do not mix; and as you've found out, even the "threads signals" apparently intended for use with threads don't work for any reasonable definition of the term.

        For example, With SAFE_SIGNALS in force, a signal, even a kill, will not interrupt a join. (That's the first problem with the code you posted in your new thread.)

        And even if you worked your way around that; you'll end up having to poll in your threads and that's a nonsense.

        Far simpler to set a shared variable true in your main thread signal handler and have your threads poll on that.

        But simpler even than that is to just detach your threads -- either when you create them or when you receive the kill signal -- and then exit your main thread. The kids will then just go away silently without any polling or other special considerations, regardless of what they are doing at the time.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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.
        .

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1029837]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2014-12-26 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (176 votes), past polls