8.210. ThreadProvides Perl multithreading support. Distributed as a beta feature with Perl 5.005 and runs only on versions of Perl that were built with thread support. Has the following functions and methods.
$t = new Thread \&sub[, params] Constructor. Starts a new thread in the referenced subroutine, \sub, returning a thread object that represents the new thread. The optional parameter list is passed to the subroutine. Execution continues in both the new thread and the code.
$t = async {block}; Creates a new thread to execute the block that follows it. The block is treated as an anonymous subroutine (and therefore has a semicolon after the closing bracket). Returns a thread object. async isn't exported by default, so you can specify use Thread qw(async); or fully qualify the name (Thread::async).
cond_broadcast var Like cond_wait, but unblocks all threads blocked in a cond_wait on the locked variable, not just one thread.
cond_signal var Takes the locked variable var and unblocks one thread that's cond_wait ing on that variable. If multiple threads are so blocked, the one that will be unblocked can't be determined.
cond_wait var Takes the locked variable var, unlocks it, and blocks until another thread does a cond_signal or cond_broadcast for that variable. The variable is relocked after the cond_wait has been satisfied. If multiple threads are cond_wait ing, all but one will reblock waiting to reacquire the lock.
eval {$t->join} Wraps an eval around a join. Waits for a thread to exit and passes any return values from the thread, putting errors into $@.
$t->join Waits for a thread to end. When it does, returns any exit values from the thread. Blocks until the thread has ended, unless the thread has already terminated.
Thread->list Returns list of thread objects for all running and finished, but not join ed, threads.
Locks a variable or a subroutine. A lock on a variable is maintained until the lock goes out of scope. If the variable is already locked by another thread, the lock call blocks until the variable is available. You can recursively lock the variable, which stays locked until the outermost lock goes out of scope. Note that locks on variables affect only other lock calls; they don't prevent normal access to a variable. Also, locking a container object (e.g., an array) doesn't lock each element of the container. Locking a subroutine blocks any calls to the subroutine until the lock goes out of scope; no other thread can access the subroutine while the lock is in effect.
Thread->self Returns an object representing the thread that made the call.
$id = $t->tid Returns an object representing the tid (thread ID) of a thread. The ID is simply an integer that is incremented each time a new thread is created, starting with zero for the main thread of a program. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|