Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Calling exit() in thread does not call exit handler

by kees (Initiate)
on Oct 30, 2014 at 12:55 UTC ( [id://1105624]=note: print w/replies, xml ) Need Help??


in reply to Re: Calling exit() in thread does not call exit handler
in thread Calling exit() in thread does not call exit handler

I don't like to catch an abnormal exit of my thread, but I use a thread to be able to force an exit of the main thread, including cleanup (that belongs to the main thread).

I know gathered all required information in a shared multi-dimensional hash table, which worked well. It just took some time to solve all invalid scalar errors that happened on runtime (because not all required data was correctly marked as shared).

Replies are listed 'Best First'.
Re^3: Calling exit() in thread does not call exit handler (broken by design)
by BrowserUk (Patriarch) on Oct 30, 2014 at 13:16 UTC
    I use a thread to be able to force an exit of the main thread

    That is really, really, really bad design -- (*)like designing a train where the only way to get off is to smash a window and jump; and hoping that there'll be somewhere soft to land -- and it will come back to bite you!

    * That's a pretty terrible analogy; maybe this is better: Like designing a satnav such that when you reach your destination, it kills your engine dead and then relies upon the anti-skid brakes and collision avoidance systems to bring you to a halt safely.

    I seriously caution you against taking this design forward. Especially when it is so simple to avoid it:

    use strict; use threads; use threads::shared; my $inthread : shared; sub doCleanUp { print "doing cleanup"; } my $threadsleeptime : shared = 2; my $mainsleeptime = 5; my $thread = async { eval { $inthread = 1; sleep($threadsleeptime); print "Sleep in thread finished\n"; die('abnormal exit'); }; if( $@ =~ /^abnormal exit/ ) { print "triggering cleanup in main\n"; return -1; } return 0; }; $thread->join() and doCleanUp() if ($thread); print "End of main program\n"; __END__ C:\test>junk29 Sleep in thread finished triggering cleanup in main doing cleanupEnd of main program

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1105624]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-03-29 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found