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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.

In reply to Re^3: Calling exit() in thread does not call exit handler (broken by design) by BrowserUk
in thread Calling exit() in thread does not call exit handler by kees

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-24 18:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found