{
    "content": [
        {
            "type": "text",
            "text": "# Thread (man)\n\n## NAME\n\nThread - Manipulate threads in Perl (for old code only)\n\n## SYNOPSIS\n\nuse 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;\n\n## DESCRIPTION\n\nThe \"Thread\" module provides multithreading support for Perl.\n\n## Sections\n\n- **NAME**\n- **DEPRECATED**\n- **HISTORY**\n- **SYNOPSIS**\n- **DESCRIPTION**\n- **FUNCTIONS**\n- **METHODS**\n- **DEFUNCT**\n- **SEE ALSO**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "Thread",
        "section": "",
        "mode": "man",
        "summary": "Thread - Manipulate threads in Perl (for old code only)",
        "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;",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "DEPRECATED",
                "lines": 14,
                "subsections": []
            },
            {
                "name": "HISTORY",
                "lines": 16,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 25,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "FUNCTIONS",
                "lines": 66,
                "subsections": []
            },
            {
                "name": "METHODS",
                "lines": 24,
                "subsections": []
            },
            {
                "name": "DEFUNCT",
                "lines": 17,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 5,
                "subsections": []
            }
        ],
        "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\nversion 5.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\nthread models are completely different, and anything to do with data sharing has to be\nthought differently.  With ithreads, you must explicitly \"share()\" variables between the\nthreads.\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\nthreads\".\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\nplatforms.\n\nIn Perl 5.8, the ithreads model became available through the \"threads\" module, and the\n5005threads 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\nlist is passed as parameters to the subroutine. Execution continues in both the\nsubroutine and 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\nwill remain 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\nthing, Perl 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\nis treated as an anonymous sub, and so must have a semi-colon after the closing\nbrace. Like \"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\nvariable, and blocks until another thread does a \"condsignal\" or \"condbroadcast\"\nfor that same locked variable. The variable that \"condwait\" blocked on is relocked\nafter the \"condwait\" is satisfied.  If there are multiple threads \"condwait\"ing on\nthe same variable, all but one will reblock waiting to re-acquire the lock on the\nvariable.  (So if you're only using \"condwait\" for synchronization, give up the lock\nas soon as possible.)\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\na \"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\nresults are implementation-dependent.\n",
                "subsections": []
            },
            "METHODS": {
                "content": "join    \"join\" waits for a thread to end and returns any values the thread exited with.\n\"join\" will block until the thread has ended, though it won't block if the thread has\nalready terminated.\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\n\"join\".\n\ndetach  \"detach\" tells a thread that it is never going to be joined i.e.  that all traces of\nits existence can be removed once it stops running.  Errors in detached threads will\nnot be visible anywhere - if you want to catch them, you should use $SIG{DIE} or\nsomething like that.\n\nequal   \"equal\" tests whether two thread objects represent the same thread and returns true\nif they 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\ntid of 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\nlock(\\&sub)\nWith 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\nserialize access to the subroutine, but allowed different threads non-simultaneous\naccess.\n\neval    The \"eval\" method wrapped an \"eval\" around a \"join\", and so waited for a thread to\nexit, passing along any values the thread might have returned and placing any errors\ninto $@.\n\nflags   The \"flags\" method returned the flags for the thread - an integer value corresponding\nto the internal flags for the thread.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "threads, threads::shared, Thread::Queue, Thread::Semaphore\n\n\n\nperl v5.34.0                                 2025-07-25                                Thread(3perl)",
                "subsections": []
            }
        }
    }
}