package threads;
use 5.008;
use strict;
use warnings;
use Config;
BEGIN {
unless ($Config{useithreads}) {
my @caller = caller(2);
die \&equal,
'fallback' => 1;
BEGIN {
warn "Warning, threads::shared has already been loaded. ".
"To enable shared variables for these modules 'use threads' ".
"must be called before any of those modules are loaded\n"
if($threads::shared::threads_shared);
}
require Exporter;
require DynaLoader;
our @ISA = qw(Exporter DynaLoader);
our %EXPORT_TAGS = ( all => [qw(yield)]);
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw(
async
);
our $VERSION = '1.05';
# || 0 to ensure compatibility with previous versions
sub equal { ($_[0]->tid == $_[1]->tid) || 0 }
# use "goto" trick to avoid pad problems from 5.8.1 (fixed in 5.8.2)
# should also be faster
sub async (&;@) { unshift @_,'threads'; goto &new }
sub object {
return undef unless @_ > 1;
foreach (threads->list) {
return $_ if $_->tid == $_[1];
}
return undef;
}
$threads::threads = 1;
bootstrap threads $VERSION;
# why document 'new' then use 'create' in the tests!
*create = \&new;
# Preloaded methods go here.
1;
__END__
=head1 NAME
threads - Perl extension allowing use of interpreter based threads from perl
=head1 SYNOPSIS
use threads;
sub start_thread {
print "Thread started\n";
}
my $thread = threads->create("start_thread","argument");
my $thread2 = $thread->create(sub { print "I am a thread"},"argument");
my $thread3 = async { foreach (@files) { ... } };
$thread->join();
$thread->detach();
$thread = threads->self();
$thread = threads->object( $tid );
$thread->tid();
threads->tid();
threads->self->tid();
threads->yield();
threads->list();
=head1 DESCRIPTION
Perl 5.6 introduced something called interpreter threads. Interpreter
threads are different from "5005threads" (the thread model of Perl
5.005) by creating a new perl interpreter per thread and not sharing
any data or state between threads by default.
Prior to perl 5.8 this has only been available to people embedding
perl and for emulating fork() on windows.
The threads API is loosely based on the old Thread.pm API. It is very
important to note that variables are not shared between threads, all
variables are per default thread local. To use shared variables one
must use threads::shared.
It is also important to note that you must enable threads by doing
C