<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package Thread;

use strict;
use warnings;
no warnings 'redefine';

our $VERSION = '3.05';
$VERSION = eval $VERSION;

BEGIN {
    use Config;
    if (! $Config{useithreads}) {
        die("This Perl not built to support threads\n");
    }
}

use threads 'yield';
use threads::shared;

require Exporter;
our @ISA = qw(Exporter threads);
our @EXPORT = qw(cond_wait cond_broadcast cond_signal);
our @EXPORT_OK = qw(async yield);

sub async (&amp;;@) { return Thread-&gt;new(shift); }

sub done { return ! shift-&gt;is_running(); }

sub eval  { die("'eval' not implemented with 'ithreads'\n"); };
sub flags { die("'flags' not implemented with 'ithreads'\n"); };

1;

__END__

=head1 NAME

Thread - Manipulate threads in Perl (for old code only)

=head1 DEPRECATED

The C&lt;Thread&gt; module served as the frontend to the old-style thread model,
called I&lt;5005threads&gt;, that was introduced in release 5.005.  That model was
deprecated, and has been removed in version 5.10.

For old code and interim backwards compatibility, the C&lt;Thread&gt; module has
been reworked to function as a frontend for the new interpreter threads
(I&lt;ithreads&gt;) model.  However, some previous functionality is not available.
Further, the data sharing models between the two thread models are completely
different, and anything to do with data sharing has to be thought differently.
With I&lt;ithreads&gt;, you must explicitly C&lt;share()&gt; variables between the
threads.

You are strongly encouraged to migrate any existing threaded code to the new
model (i.e., use the C&lt;threads&gt; and C&lt;threads::shared&gt; modules) as soon as
possible.

=head1 HISTORY

In Perl 5.005, the thread model was that all data is implicitly shared, and
shared access to data has to be explicitly synchronized.  This model is called
I&lt;5005threads&gt;.

In Perl 5.6, a new model was introduced in which all is was thread local and
shared access to data has to be explicitly declared.  This model is called
I&lt;ithreads&gt;, for "interpreter threads".

In Perl 5.6, the I&lt;ithreads&gt; model was not available as a public API; only as
an internal API that was available for extension writers, and to implement
fork() emulation on Win32 platforms.

In Perl 5.8, the I&lt;ithreads&gt; model became available through the C&lt;threads&gt;
module, and the I&lt;5005threads&gt; model was deprecated.

In Perl 5.10, the I&lt;5005threads&gt; model was removed from the Perl interpreter.

=head1 SYNOPSIS

    use Thread qw(:DEFAULT async yield);

    my $t = Thread-&gt;new(\&amp;start_sub, @start_args);

    $result = $t-&gt;join;
    $t-&gt;detach;

    if ($t-&gt;done) {
        $t-&gt;join;
    }

    if($t-&gt;equal($another_thread)) {
        # ...
    }

    yield();

    my $tid = Thread-&gt;self-&gt;tid;

    lock($scalar);
    lock(@array);
    lock(%hash);

    my @list = Thread-&gt;list;

=head1 DESCRIPTION

The C&lt;Thread&gt; module provides multithreading support for Perl.

=head1 FUNCTIONS

=over 8

=item $thread = Thread-&gt;new(\&amp;start_sub)

=item $thread = Thread-&gt;new(\&amp;start_sub, LIST)

C&lt;new&gt; starts a new thread of execution in the referenced subroutine. The
optional list is passed as parameters to the subroutine. Execution
continues in both the subroutine and the code after the C&lt;new&gt; call.

C&lt;&lt; Thread-&gt;new &gt;&gt; returns a thread object representing the newly created
thread.

=item lock VARIABLE

C&lt;lock&gt; places a lock on a variable until the lock goes out of scope.

If the variable is locked by another thread, the C&lt;lock&gt; call will
block until it's available.  C&lt;lock&gt; is recursive, so multiple calls
to C&lt;lock&gt; are safe--the variable will remain locked until the
outermost lock on the variable goes out of scope.

Locks on variables only affect C&lt;lock&gt; calls--they do I&lt;not&gt; affect normal
access to a variable. (Locks on subs are different, and covered in a bit.)
If you really, I&lt;really&gt; want locks to block access, then go ahead and tie
them to something and manage this yourself.  This is done on purpose.
While managing access to variables is a good thing, Perl doesn't force
you out of its living room...

If a container object, such as a hash or array, is locked, all the
elements of that container are not locked. For example, if a thread
does a C&lt;lock @a&gt;, any other thread doing a C&lt;lock($a[12])&gt; won't
block.

Finally, C&lt;lock&gt; will traverse up references exactly I&lt;one&gt; level.
C&lt;lock(\$a)&gt; is equivalent to C&lt;lock($a)&gt;, while C&lt;lock(\\$a)&gt; is not.

=item async BLOCK;

C&lt;async&gt; creates a thread to execute the block immediately following
it.  This block is treated as an anonymous sub, and so must have a
semi-colon after the closing brace. Like C&lt;&lt; Thread-&gt;new &gt;&gt;, C&lt;async&gt;
returns a thread object.

=item Thread-&gt;self

The C&lt;Thread-E&lt;gt&gt;self&gt; function returns a thread object that represents
the thread making the C&lt;Thread-E&lt;gt&gt;self&gt; call.

=item Thread-&gt;list

Returns a list of all non-joined, non-detached Thread objects.

=item cond_wait VARIABLE

The C&lt;cond_wait&gt; function takes a B&lt;locked&gt; variable as
a parameter, unlocks the variable, and blocks until another thread
does a C&lt;cond_signal&gt; or C&lt;cond_broadcast&gt; for that same locked
variable. The variable that C&lt;cond_wait&gt; blocked on is relocked
after the C&lt;cond_wait&gt; is satisfied.  If there are multiple threads
C&lt;cond_wait&gt;ing on the same variable, all but one will reblock waiting
to re-acquire the lock on the variable.  (So if you're only using
C&lt;cond_wait&gt; for synchronization, give up the lock as soon as
possible.)

=item cond_signal VARIABLE

The C&lt;cond_signal&gt; function takes a locked variable as a parameter and
unblocks one thread that's C&lt;cond_wait&gt;ing on that variable. If more than
one thread is blocked in a C&lt;cond_wait&gt; on that variable, only one (and
which one is indeterminate) will be unblocked.

If there are no threads blocked in a C&lt;cond_wait&gt; on the variable,
the signal is discarded.

=item cond_broadcast VARIABLE

The C&lt;cond_broadcast&gt; function works similarly to C&lt;cond_signal&gt;.
C&lt;cond_broadcast&gt;, though, will unblock B&lt;all&gt; the threads that are
blocked in a C&lt;cond_wait&gt; on the locked variable, rather than only
one.

=item yield

The C&lt;yield&gt; function allows another thread to take control of the
CPU. The exact results are implementation-dependent.

=back

=head1 METHODS

=over 8

=item join

C&lt;join&gt; waits for a thread to end and returns any values the thread
exited with.  C&lt;join&gt; will block until the thread has ended, though
it won't block if the thread has already terminated.

If the thread being C&lt;join&gt;ed C&lt;die&gt;d, the error it died with will
be returned at this time. If you don't want the thread performing
the C&lt;join&gt; to die as well, you should either wrap the C&lt;join&gt; in
an C&lt;eval&gt; or use the C&lt;eval&gt; thread method instead of C&lt;join&gt;.

=item detach

C&lt;detach&gt; tells a thread that it is never going to be joined i.e.
that all traces of its existence can be removed once it stops running.
Errors in detached threads will not be visible anywhere - if you want
to catch them, you should use $SIG{__DIE__} or something like that.

=item equal

C&lt;equal&gt; tests whether two thread objects represent the same thread and
returns true if they do.

=item tid

The C&lt;tid&gt; method returns the tid of a thread. The tid is
a monotonically increasing integer assigned when a thread is
created. The main thread of a program will have a tid of zero,
while subsequent threads will have tids assigned starting with one.

=item done

The C&lt;done&gt; method returns true if the thread you're checking has
finished, and false otherwise.

=back

=head1 DEFUNCT

The following were implemented with I&lt;5005threads&gt;, but are no longer
available with I&lt;ithreads&gt;.

=over 8

=item lock(\&amp;sub)

With 5005threads, you could also C&lt;lock&gt; a sub such that any calls to that sub
from another thread would block until the lock was released.

Also, subroutines could be declared with the C&lt;:locked&gt; attribute which would
serialize access to the subroutine, but allowed different threads
non-simultaneous access.

=item eval

The C&lt;eval&gt; method wrapped an C&lt;eval&gt; around a C&lt;join&gt;, and so waited for a
thread to exit, passing along any values the thread might have returned and
placing any errors into C&lt;$@&gt;.

=item flags

The C&lt;flags&gt; method returned the flags for the thread - an integer value
corresponding to the internal flags for the thread.

=back

=head1 SEE ALSO

L&lt;threads&gt;, L&lt;threads::shared&gt;, L&lt;Thread::Queue&gt;, L&lt;Thread::Semaphore&gt;

=cut
</pre></body></html>