The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Thread::Exit - provide thread-local exit() and END {}

SYNOPSIS

    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"

DESCRIPTION

                  *** 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.

MOD_PERL

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.

This may need further fine-tuning when I've actually tried this with Apache myself.

CAVEATS

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.

TODO

Examples should be added.

AUTHOR

Elizabeth Mattijsen, <liz@dijkmat.nl>.

Please report bugs to <perlbugs@dijkmat.nl>.

COPYRIGHT

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.

SEE ALSO

threads, Thread::Serialize.