Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Kill and free the memory Never ending threads from main

by meena (Novice)
on Aug 24, 2013 at 06:27 UTC ( #1050766=perlquestion: print w/ replies, xml ) Need Help??
meena has asked for the wisdom of the Perl Monks concerning the following question:

There is main running and two threads running,The two threads are infinite event loops where we are waiting for some event, The issue is I want to wait for the event to come for some finite amount of time and then exit from the program But when I am exiting the threads are still running I want to free all the space occupied by this threads and then exit from the script How to fix this? How to do a clean up before I exit How to kill threads thats are never ending

Comment on Kill and free the memory Never ending threads from main
Re: Kill and free the memory Never ending threads from main
by moritz (Cardinal) on Aug 24, 2013 at 06:35 UTC

    When you terminate the process, your operating system will clean up all the other threads for you.

    To quote perlthrtut:

    An action that terminates a process will terminate all running threads. die() and exit() have this property, and perl does an exit when the main thread exits, perhaps implicitly by falling off the end of your code, even if that's not what you want.

    The rest of that section might also be relevant for you.

Re: Kill and free the memory Never ending threads from main
by BrowserUk (Pope) on Aug 24, 2013 at 06:35 UTC
    But when I am exiting the threads are still running I want to free all the space occupied by this threads and then exit from the script

    When your main thread exits; any other threads will be killed and cleaned up automatically.

    If you wish to suppress this oh so useful informational message:

    C:\Downloaded\testp>perl -Mthreads -E"async{ sleep; }; say 'hi & bye'; hi & bye Perl exited with active threads: 1 running and unjoined 0 finished and unjoined 0 running and detached

    Just detach the threads:

    C:\Downloaded\testp>perl -Mthreads -E"async{ sleep; }->detach; say 'hi + & bye';" hi & bye

    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.
Re: Kill and free the memory Never ending threads from main
by sundialsvc4 (Abbot) on Aug 25, 2013 at 14:51 UTC

    Approach the problem this way:   the event just means, “you should not be asleep right now.”   (Not “please wake up.”)   The main-loop of the thread is driven by a set of shared flags which tell it what it should do next, one of the choices being “die now.”

    while (1) { if (shared.die) { exit; } elsif (shared.do_this) { do_this; } elsif (shared.do_that) { do_that; } ... else { wait_for_event(); } }

    The parent thread first sets one of the flags, then strobes the event to be sure that the child is not-asleep to respond to it.

    You don’t need to invent these things from scratch.   Look at existing workhorses such as POE.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2014-12-20 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (96 votes), past polls