Thread(3perl)
π Thread β manipulating threads in Perl (for old code only) π
hi bestie, welcome to Thread(3perl), the page from the Perl Programmers Reference Guide that's essentially a haunted house tour of Perl threading's past. buckle up, it's giving nostalgic and slightly cursed π«Ά
π DEPRECATED π
okay so first thing, and I cannot stress this enough: the Thread module was the frontend to the old-style thread model, called 5005threads, which got introduced in release 5.005. that model got deprecated, and it has been removed in version 5.10. gone. no more. β¨
but for old code and for interim backwards compatibility, the Thread module has been reworked to function as a frontend for the new interpreter threads (ithreads) model. the catch? some previous functionality is just... not available anymore π
and further β and this is the part that really gets people β 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 ithreads, you must explicitly share() variables between the threads. like, explicitly-explicitly.
β οΈ you are strongly encouraged to migrate any existing threaded code to the new model (i.e., use thethreadsandthreads::sharedmodules) as soon as possible. like, today. right now. no cap.
πΈ 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 5005threads.
- In Perl 5.6, a new model was introduced in which all data is 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. secret menu energy π - In Perl 5.8, the ithreads model became available through the
threadsmodule, and the 5005threads model was deprecated. - In Perl 5.10, the 5005threads model was removed from the Perl interpreter. rip π
β¨ SYNOPSIS β¨
use Thread qw(:DEFAULT async yield);
my $t = Thread->new(\&start_sub, @start_args);
$result = $t->join;
$t->detach;
if ($t->done) {
$t->join;
}
if($t->equal($another_thread)) {
# ...
}
yield();
my $tid = Thread->self->tid;
lock($scalar);
lock(@array);
lock(%hash);
my @list = Thread->list;
π¦ DESCRIPTION π¦
The Thread module provides multithreading support for Perl. that's it, that's the whole vibe π
π FUNCTIONS π
$thread = Thread->new(\&start_sub)$thread = Thread->new(\&start_sub, LIST)βnewstarts 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 thenewcall. π
Thread->new returns a thread object representing the newly created thread.
lock VARIABLEβlockplaces a lock on a variable until the lock goes out of scope.
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. nesting, but make it legal π«Ά
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. sneaky!
finally, lock will traverse up references exactly one level. lock(\$a) is equivalent to lock($a), while lock(\\$a) is not. one level, bestie, not two.
async BLOCK;βasynccreates 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. likeThread->new,asyncreturns a thread object. don't forget the;, i'm serious πThread->selfβ returns a thread object that represents the thread making theThread->selfcall. main character energy, literally, it's you πThread->listβ returns a list of all non-joined, non-detachedThreadobjects.cond_wait VARIABLEβ takes a locked variable as a parameter, unlocks the variable, and blocks until another thread does acond_signalorcond_broadcastfor that same locked variable. the variable thatcond_waitblocked on is relocked after thecond_waitis satisfied. if there are multiple threadscond_waiting on the same variable, all but one will reblock waiting to re-acquire the lock on the variable. (so if you're only usingcond_waitfor synchronization, give up the lock as soon as possible.) π§cond_signal VARIABLEβ takes a locked variable as a parameter and unblocks one thread that'scond_waiting on that variable. if more than one thread is blocked in acond_waiton that variable, only one (and which one is indeterminate) will be unblocked. mystery pick, no spoilers π²
if there are no threads blocked in a cond_wait on the variable, the signal is discarded. into the void it goes π
cond_broadcast VARIABLEβ works similarly tocond_signal.cond_broadcast, though, will unblock all the threads that are blocked in acond_waiton the locked variable, rather than only one. everybody's invited πyieldβ allows another thread to take control of the CPU. the exact results are implementation-dependent. ymmv, girlie β¨
π METHODS π
joinβ waits for a thread to end and returns any values the thread exited with.joinwill block until the thread has ended, though it won't block if the thread has already terminated.
if the thread being joined died, 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. (and yes, that eval thread method is in DEFUNCT below, so π)
detachβ 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.
equalβ tests whether two thread objects represent the same thread and returns true if they do.tidβ 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.doneβ returns true if the thread you're checking has finished, and false otherwise. simple. iconic.
π DEFUNCT π
the following were implemented with 5005threads, but are no longer available with ithreads. rip to these, they were doing their best π―οΈ
lock(\&sub)β with 5005threads, you could alsolocka 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 :locked attribute which would serialize access to the subroutine, but allowed different threads non-simultaneous access.
evalβ theevalmethod wrapped anevalaround ajoin, and so waited for a thread to exit, passing along any values the thread might have returned and placing any errors into$@.flagsβ theflagsmethod returned the flags for the thread β an integer value corresponding to the internal flags for the thread.
π SEE ALSO π
threads, threads::shared, Thread::Queue, Thread::Semaphore
and that's the whole page, bestie π«Ά this was Thread(3perl) from perl v5.44.0, dated 2026-10-17 β go forth, migrate your old threaded code, and slay π
β¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms