This and other RFCs are available on the web at
http://dev.perl.org/rfc/
=head1 TITLE
Thread Programming Model
=head1 VERSION
Maintainer: Steven McDougall [EMAIL PROTECTED]
Date: 31 Aug 2000
Last Modified: 26 Sep 2000
Mailing List: [EMAIL PROTECTED]
Number: 185
Version: 3
Status: Frozen
=head1 ABSTRACT
This RFC describes the programming interface to Perl6 threads.
It documents the function calls, operators, classes, methods, or
whatever else the language provides for programming with threads.
=head1 CHANGES
=head2 v3
Frozen
=head2 v2
=over 4
=item *
Added SYNOPSIS, and wrote a proper ABSTRACT
=item *
Detailed Casync
=item *
Detailed sharing of lexicals between threads
=item *
Traded Mutexes back for Clock, Ctry, and Cunlock
=item *
Pushed CSemaphore, CEvent, and CTimer down into CThread::
=item *
Specified readable, writable and failure to return Events
=item *
Reworked the wait functions
=item *
Added CQueue
=back
=head1 FREEZE
There was little, if any, further discussion after version 2.
=head1 SYNOPSIS
use Thread;
$sub = sub { ... };
$thread = new Thread \func , @args;
$thread = new Thread $sub, @args;
$thread = new Thread sub { ... }, @args;
async { ... };
$result = join $thread;
$thread = this Thread;
@threads = all Thread;
$thread1 == $thread2 and ...
Thread::yield();
critical { ... }; # one thread at a time in this block
lock $scalar;
lock @array
lock %hash;
lock sub;
$ok = try $scalar;
$ok = try @array
$ok = try %hash;
$ok = try sub;
unlock $scalar;
unlock @array
unlock %hash;
unlock sub;
$event = auto Thread::Event;
$event = manual Thread::Event;
set$event;
reset $event;
wait $event;
$semaphore = new Thread::Semaphore $initial;
$ok= $semaphore-up($n);
$semaphore-down;
$count = $semaphore-count;
$timer = Thread::Timer-delay($seconds);
$timer = Thread::Timer-alarm($time);
$timer-wait;
$event = $fh-readable
$event = $fh-writable
$event = $fh-failure
$ok = wait_all(@references);
$i = wait_any(@references);
$queue = new Thread::Queue
$queue-enqueue($a);
$a = $queue-dequeue;
$empty = $queue-empty;
=head1 DESCRIPTION
=head2 Thread
=over 4
=item I$thread = Cnew CThread \Ifunc, I@args
Executes Ifunc(I@args) in a separate thread. The return value is
a reference to the CThread object that manages the thread.
The subroutine executes in its enclosing lexical context. This means
that lexical variables declared in that context may be shared between
threads. See RFC 178 for examples.
=item I$thread = Cnew CThread I$sub, I@args
=item I$thread = Cnew CThread Csub { ... }, I@args
Executes an anonymous subroutine in a separate thread, passing it
I@args. The return value is a reference to the CThread object that
manages the thread.
The subroutine is a closure. References to variables in its lexical
context are bound when the Csub operator executes. See RFC 178 for
examples.
=item Casync BLOCK
Executes BLOCK in a separate thread. Syntactically, Casync BLOCK
works like Cdo BLOCK. Casync creates a CThread object to manage
the thread, but it does not return a reference to it. If you want the
CThread object, use one of the Cnew CThread forms shown above.
The BLOCK executes in its enclosing lexical context. This means that
lexical variables declared in that context may be shared between
threads.
=item I$thread = Cthis CThread
Returns a reference to the CThread object that manages the current
thread.
=item I@threads = Call CThread
Returns a list of references to all existing CThread objects in the
program. This includes CThread objects created for Casync blocks.
=item I$result = Cjoin I$thread
=item I@result = Cjoin I$thread
Blocks until I$thread terminates. May be called repeatedly,
by any number of threads.
Returns the last expression evaluated in I$thread. This expression
is evaluated in list context inside the thread.
If Cjoin is called in list context, it returns the entire list; if
Cjoin is called in scalar context, it returns the first element of
the list.
=item I$thread1 == I$thread2
Evaluates to true iff I$thread1 and I$thread2 reference the same
CThread object.
=item CThread::yield()
Gives the interpreter an opportunity to switch to another thread. The
interpreter is not obligated to take this opportunity, and the calling
thread may regain control after an arbitrarily short period of time.
=back
=head2 Critical section
Ccritical is a new keyword. Syntactically, it works like Cdo.
critical { ... };
The interpreter guarantees that only one thread at a time can execute
a Ccritical block.
=head2 Lock
=over 4
=item Clock I$scalar
=item Clock I@array
=item Clock I%hash
=item Clock Isub
Applies a lock to a variable.
If there are no locks applied to