Thread::Exit - provide thread-local exit() and END {}
use Thread::Exit (); # just make exit() thread local use Thread::Exit end => 'end_sub', # set sub to exec at end of thread (default: none) automatic => 1, # make all new threads end the same (default: 0) ; Thread::Exit->end( \$end_sub ); # set/adapt END sub later Thread::Exit->end( undef ); # disable END sub $end = Thread::Exit->end; Thread::Exit->automatic( 1 ); # make all new threads use this end sub Thread::Exit->automatic( 0 ); # new threads won't use this end sub $automatic = Thread::Exit->automatic; $thread = threads->new( sub { exit( "We've exited" ) } ); print $thread->join; # prints "We've exited"
*** A note of CAUTION *** This module only functions on Perl versions 5.8.0 and later. And then only when threads are enabled with -Dusethreads. It is of no use with any version of Perl before 5.8.0 or without threads enabled. *************************
This module add two features to threads that are sorely missed by some.
The first feature is that you can use exit() within a thread to return() from that thread only. Without this module, exit() stops all threads and exits to the calling process (which usually is the operating system). With this module, exit() functions just as return() (including passing back values to the parent thread).
The second feature is that you can specify a subroutine that will be executed after the thread is done, but before the thread returns to the parent thread. Multiple "end" subroutines can be chained together if necessary.
To allow this module to function under Apache with mod_perl, a special check is included for the existence of the Apache::exit() subroutine. If that exists, that exit routine will be preferred above the CORE::exit() routine when exiting from the thread in which the first use Thread::Exit occurred.
use Thread::Exit
This may need further fine-tuning when I've actually tried this with Apache myself.
Because transport of data structures between threads is severely limited in the current threads implementation (perl 5.8.0), data structures need to be serialized. This is achieved by using the Thread::Serialize library. Please check that module for information about the limitations (of any) of data structure transport between threads.
Examples should be added.
Elizabeth Mattijsen, <liz@dijkmat.nl>.
Please report bugs to <perlbugs@dijkmat.nl>.
Copyright (c) 2002 Elizabeth Mattijsen <liz@dijkmat.nl>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
threads, Thread::Serialize.
To install Thread::Exit, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Thread::Exit
CPAN shell
perl -MCPAN -e shell install Thread::Exit
For more information on module installation, please visit the detailed CPAN module installation guide.