{
    "mode": "perldoc",
    "parameter": "Thread",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Thread/json",
    "generated": "2026-06-11T21:26:20Z",
    "synopsis": "use Thread qw(:DEFAULT async yield);\nmy $t = Thread->new(\\&startsub, @startargs);\n$result = $t->join;\n$t->detach;\nif ($t->done) {\n$t->join;\n}\nif($t->equal($anotherthread)) {\n# ...\n}\nyield();\nmy $tid = Thread->self->tid;\nlock($scalar);\nlock(@array);\nlock(%hash);\nmy @list = Thread->list;",
    "sections": {
        "NAME": {
            "content": "Thread - Manipulate threads in Perl (for old code only)\n",
            "subsections": []
        },
        "DEPRECATED": {
            "content": "The \"Thread\" module served as the frontend to the old-style thread model, called *5005threads*,\nthat was introduced in release 5.005. That model was deprecated, and has been removed in version\n5.10.\n\nFor old code and interim backwards compatibility, the \"Thread\" module has been reworked to\nfunction as a frontend for the new interpreter threads (*ithreads*) model. However, some\nprevious functionality is not available. Further, the data sharing models between the two thread\nmodels are completely different, and anything to do with data sharing has to be thought\ndifferently. With *ithreads*, you must explicitly \"share()\" variables between the threads.\n\nYou are strongly encouraged to migrate any existing threaded code to the new model (i.e., use\nthe \"threads\" and \"threads::shared\" modules) as soon as possible.\n",
            "subsections": []
        },
        "HISTORY": {
            "content": "In Perl 5.005, the thread model was that all data is implicitly shared, and shared access to\ndata has to be explicitly synchronized. This model is called *5005threads*.\n\nIn Perl 5.6, a new model was introduced in which all is was thread local and shared access to\ndata has to be explicitly declared. This model is called *ithreads*, for \"interpreter threads\".\n\nIn Perl 5.6, the *ithreads* model was not available as a public API; only as an internal API\nthat was available for extension writers, and to implement fork() emulation on Win32 platforms.\n\nIn Perl 5.8, the *ithreads* model became available through the \"threads\" module, and the\n*5005threads* model was deprecated.\n\nIn Perl 5.10, the *5005threads* model was removed from the Perl interpreter.\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Thread qw(:DEFAULT async yield);\n\nmy $t = Thread->new(\\&startsub, @startargs);\n\n$result = $t->join;\n$t->detach;\n\nif ($t->done) {\n$t->join;\n}\n\nif($t->equal($anotherthread)) {\n# ...\n}\n\nyield();\n\nmy $tid = Thread->self->tid;\n\nlock($scalar);\nlock(@array);\nlock(%hash);\n\nmy @list = Thread->list;\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The \"Thread\" module provides multithreading support for Perl.\n",
            "subsections": []
        },
        "FUNCTIONS": {
            "content": "$thread = Thread->new(\\&startsub)\n$thread = Thread->new(\\&startsub, LIST)\n\"new\" starts a new thread of execution in the referenced subroutine. The optional list\nis passed as parameters to the subroutine. Execution continues in both the subroutine\nand the code after the \"new\" call.\n\n\"Thread->new\" returns a thread object representing the newly created thread.\n\nlock VARIABLE\n\"lock\" places a lock on a variable until the lock goes out of scope.\n\nIf the variable is locked by another thread, the \"lock\" call will block until it's\navailable. \"lock\" is recursive, so multiple calls to \"lock\" are safe--the variable will\nremain locked until the outermost lock on the variable goes out of scope.\n\nLocks on variables only affect \"lock\" calls--they do *not* affect normal access to a\nvariable. (Locks on subs are different, and covered in a bit.) If you really, *really*\nwant locks to block access, then go ahead and tie them to something and manage this\nyourself. This is done on purpose. While managing access to variables is a good thing,\nPerl doesn't force you out of its living room...\n\nIf a container object, such as a hash or array, is locked, all the elements of that\ncontainer are not locked. For example, if a thread does a \"lock @a\", any other thread\ndoing a \"lock($a[12])\" won't block.\n\nFinally, \"lock\" will traverse up references exactly *one* level. \"lock(\\$a)\" is\nequivalent to \"lock($a)\", while \"lock(\\\\$a)\" is not.\n\nasync BLOCK;\n\"async\" creates a thread to execute the block immediately following it. This block is\ntreated as an anonymous sub, and so must have a semi-colon after the closing brace. Like\n\"Thread->new\", \"async\" returns a thread object.\n\nThread->self\nThe \"Thread->self\" function returns a thread object that represents the thread making\nthe \"Thread->self\" call.\n\nThread->list\nReturns a list of all non-joined, non-detached Thread objects.\n\ncondwait VARIABLE\nThe \"condwait\" function takes a locked variable as a parameter, unlocks the variable,\nand blocks until another thread does a \"condsignal\" or \"condbroadcast\" for that same\nlocked variable. The variable that \"condwait\" blocked on is relocked after the\n\"condwait\" is satisfied. If there are multiple threads \"condwait\"ing on the same\nvariable, all but one will reblock waiting to re-acquire the lock on the variable. (So\nif you're only using \"condwait\" for synchronization, give up the lock as soon as\npossible.)\n\ncondsignal VARIABLE\nThe \"condsignal\" function takes a locked variable as a parameter and unblocks one\nthread that's \"condwait\"ing on that variable. If more than one thread is blocked in a\n\"condwait\" on that variable, only one (and which one is indeterminate) will be\nunblocked.\n\nIf there are no threads blocked in a \"condwait\" on the variable, the signal is\ndiscarded.\n\ncondbroadcast VARIABLE\nThe \"condbroadcast\" function works similarly to \"condsignal\". \"condbroadcast\",\nthough, will unblock all the threads that are blocked in a \"condwait\" on the locked\nvariable, rather than only one.\n\nyield   The \"yield\" function allows another thread to take control of the CPU. The exact results\nare implementation-dependent.\n",
            "subsections": []
        },
        "METHODS": {
            "content": "join    \"join\" waits for a thread to end and returns any values the thread exited with. \"join\"\nwill block until the thread has ended, though it won't block if the thread has already\nterminated.\n\nIf the thread being \"join\"ed \"die\"d, the error it died with will be returned at this\ntime. If you don't want the thread performing the \"join\" to die as well, you should\neither wrap the \"join\" in an \"eval\" or use the \"eval\" thread method instead of \"join\".\n\ndetach  \"detach\" tells a thread that it is never going to be joined i.e. that all traces of its\nexistence can be removed once it stops running. Errors in detached threads will not be\nvisible anywhere - if you want to catch them, you should use $SIG{DIE} or something\nlike that.\n\nequal   \"equal\" tests whether two thread objects represent the same thread and returns true if\nthey do.\n\ntid     The \"tid\" method returns the tid of a thread. The tid is a monotonically increasing\ninteger assigned when a thread is created. The main thread of a program will have a tid\nof zero, while subsequent threads will have tids assigned starting with one.\n\ndone    The \"done\" method returns true if the thread you're checking has finished, and false\notherwise.\n",
            "subsections": []
        },
        "DEFUNCT": {
            "content": "The following were implemented with *5005threads*, but are no longer available with *ithreads*.\n",
            "subsections": [
                {
                    "name": "lock",
                    "content": "With 5005threads, you could also \"lock\" a sub such that any calls to that sub from\nanother thread would block until the lock was released.\n\nAlso, subroutines could be declared with the \":locked\" attribute which would serialize\naccess to the subroutine, but allowed different threads non-simultaneous access.\n\neval    The \"eval\" method wrapped an \"eval\" around a \"join\", and so waited for a thread to exit,\npassing along any values the thread might have returned and placing any errors into $@.\n\nflags   The \"flags\" method returned the flags for the thread - an integer value corresponding to\nthe internal flags for the thread.\n"
                }
            ]
        },
        "SEE ALSO": {
            "content": "threads, threads::shared, Thread::Queue, Thread::Semaphore\n",
            "subsections": []
        }
    },
    "summary": "Thread - Manipulate threads in Perl (for old code only)",
    "flags": [],
    "examples": [],
    "see_also": []
}