Mass Deface
.
See also L"Thread-Safety of System Libraries">.
=head1 Thread Basics
The L module provides the basic functions you need to write
threaded programs. In the following sections, we'll cover the basics,
showing you what you need to do to create a threaded program. After
that, we'll go over some of the features of the L module that
make threaded programming easier.
=head2 Basic Thread Support
Thread support is a Perl compile-time option. It's something that's
turned on or off when Perl is built at your site, rather than when
your programs are compiled. If your Perl wasn't compiled with thread
support enabled, then any attempt to use threads will fail.
Your programs can use the Config module to check whether threads are
enabled. If your program can't run without them, you can say something
like:
use Config;
$Config{useithreads} or die('Recompile Perl with threads to run this program.');
A possibly-threaded program using a possibly-threaded module might
have code like this:
use Config;
use MyMod;
BEGIN {
if ($Config{useithreads}) {
# We have threads
require MyMod_threaded;
import MyMod_threaded;
} else {
require MyMod_unthreaded;
import MyMod_unthreaded;
}
}
Since code that runs both with and without threads is usually pretty
messy, it's best to isolate the thread-specific code in its own
module. In our example above, that's what C is, and it's
only imported if we're running on a threaded Perl.
=head2 A Note about the Examples
In a real situation, care should be taken that all threads are finished
executing before the program exits. That care has B been taken in these
examples in the interest of simplicity. Running these examples I will
produce error messages, usually caused by the fact that there are still
threads running when the program exits. You should not be alarmed by this.
=head2 Creating Threads
The L module provides the tools you need to create new
threads. Like any other module, you need to tell Perl that you want to use
it; C