Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How to simply terminate all running threads?

by Anonymous Monk
on Sep 03, 2015 at 19:38 UTC ( [id://1140911]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a script that runs a test on a list of test_objects. These tests all run concurrently using threads. Should a test fail at anytime, I would like to stop all other running threads and safely exit out of the program.
sub test1252 { my @jobs = (); foreach my $test_obj (@objects) { push @jobs, async { my $rc = test($test_obj); if ($rc ne 0) { print "Test Failed for $test_obj\n"; local $SIG{'KILL'} = sub {threads->exit();}; $_->kill('KILL')->join() for @jobs; exit $code_blue; } }; } $_->join for @jobs; }
I get something like this using 'kill' with the handler
Perl exited with active threads: 9 running and unjoined 0 finished and unjoined 0 running and detached
It doesn't appear that all the threads have died upon program exit, so I'm sure I'm not doing something right...

Replies are listed 'Best First'.
Re: How to simply terminate all running threads?
by BrowserUk (Patriarch) on Sep 03, 2015 at 20:00 UTC
    I get something like this using 'kill' with the handler 01 Perl exited with active threads: 02 9 running and unjoined 03 0 finished and unjoined 04 0 running and detached

    That is simply an informational message.

    If all you want to do is terminate the process; forget all that kill stuff and just exit. You'll get the informational message; but it is just information.

    The kill method will not interrupt the threads. It is effectively useless.


    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". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
    I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
      Would that be an unsafe tactic to just exit? Shouldn't I need to force all the threads to formally return and deallocate any resources before exiting the program? (Please excuse my ignorance on this topic as I'm not so sure about these things) Thank you for your advice.
        Would that be an unsafe tactic to just exit? Shouldn't I need to force all the threads to formally return and deallocate any resources before exiting the program?

        No. They'll get cleaned up anyway when the process goes away.

        I wouldn't recommend that for production code other than in exceptional circumstances; but for testing it ought to be fine.


        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". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.
        I'm with torvalds on this Agile (and TDD) debunked I told'em LLVM was the way to go. But did they listen!
Re: How to simply terminate all running threads?
by Anonymous Monk on Sep 04, 2015 at 00:03 UTC
    If the threads/processes are simply doing tests and not writing to anything, such that they will not leave anything in an inconsistent state, then you can simply terminate the parent process and the children will necessarily die. C'est la guerre. No permanent harm will be caused. Ignore the message.

        Why is he wrong? Is there such a thing as an orphaned thread?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-20 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found