Thread - manipulate threads in Perl (for old code only)
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 ``5005threads''.
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 ``ithreads'', for ``interpreter threads''.
In Perl 5.6 the ithreads 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 ithreads model became available through the "threads" module.
Neither model is configured by default into Perl (except, as mentioned above, in Win32 ithreads are always available.) You can see your Perl's threading configuration by running "perl -V" and looking for the use...threads variables, or inside script by "use Config;" and testing for $Config{use5005threads} and $Config{useithreads}.
For old code and interim backwards compatibility, the Thread module has been reworked to function as a frontend for both 5005threads and ithreads.
Note that the compatibility is not complete: because the data sharing models are directly opposed, anything to do with data sharing has to be thought differently. With the ithreads you must explicitly share() variables between the threads.
For new code the use of the "Thread" module is discouraged and the direct use of the "threads" and "threads::shared" modules is encouraged instead.
Finally, note that there are many known serious problems with the 5005threads, one of the least of which is that regular expression match variables like $1 are not threadsafe, that is, they easily get corrupted by competing threads. Other problems include more insidious data corruption and mysterious crashes. You are seriously urged to use ithreads instead.
use Thread;
my $t = Thread->new(\&start_sub, @start_args);
$result = $t->join; $result = $t->eval; $t->detach;
if ($t->done) { $t->join; }
if($t->equal($another_thread)) { # ... }
yield();
my $tid = Thread->self->tid;
lock($scalar); lock(@array); lock(%hash);
lock(\&sub); # not available with ithreads
$flags = $t->flags; # not available with ithreads
my @list = Thread->list; # not available with ithreads
use Thread 'async';
"Thread->new" returns a thread object representing the newly created thread.
If the variable is locked by another thread, the "lock" call will block until it's available. "lock" is recursive, so multiple calls to "lock" are safe---the variable will remain locked until the outermost lock on the variable goes out of scope.
Locks on variables only affect "lock" calls---they do not affect normal access to a variable. (Locks on subs are different, and covered in a bit.) If you really, really 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 "lock @a", any other thread doing a "lock($a[12])" won't block.
With 5005threads you may also "lock" a sub, using "lock &sub". Any calls to that sub from another thread will block until the lock is released. This behaviour is not equivalent to declaring the sub with the "locked" attribute. The "locked" attribute serializes access to a subroutine, but allows different threads non-simultaneous access. "lock &sub", on the other hand, will not allow any other thread access for the duration of the lock.
Finally, "lock" will traverse up references exactly one level. "lock(\$a)" is equivalent to "lock($a)", while "lock(\\$a)" is not.
If there are no threads blocked in a "cond_wait" on the variable, the signal is discarded.
If the thread being "join"ed "die"d, the error it died with will be returned at this time. If you don't want the thread performing the "join" to die as well, you should either wrap the "join" in an "eval" or use the "eval" thread method instead of "join".
attributes, Thread::Queue, Thread::Semaphore, Thread::Specific (not available with ithreads)
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |