| 1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
| 2 | .\" |
| 3 | .\" Standard preamble: |
| 4 | .\" ======================================================================== |
| 5 | .de Sh \" Subsection heading |
| 6 | .br |
| 7 | .if t .Sp |
| 8 | .ne 5 |
| 9 | .PP |
| 10 | \fB\\$1\fR |
| 11 | .PP |
| 12 | .. |
| 13 | .de Sp \" Vertical space (when we can't use .PP) |
| 14 | .if t .sp .5v |
| 15 | .if n .sp |
| 16 | .. |
| 17 | .de Vb \" Begin verbatim text |
| 18 | .ft CW |
| 19 | .nf |
| 20 | .ne \\$1 |
| 21 | .. |
| 22 | .de Ve \" End verbatim text |
| 23 | .ft R |
| 24 | .fi |
| 25 | .. |
| 26 | .\" Set up some character translations and predefined strings. \*(-- will |
| 27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left |
| 28 | .\" double quote, and \*(R" will give a right double quote. | will give a |
| 29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to |
| 30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' |
| 31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. |
| 32 | .tr \(*W-|\(bv\*(Tr |
| 33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' |
| 34 | .ie n \{\ |
| 35 | . ds -- \(*W- |
| 36 | . ds PI pi |
| 37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch |
| 38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch |
| 39 | . ds L" "" |
| 40 | . ds R" "" |
| 41 | . ds C` "" |
| 42 | . ds C' "" |
| 43 | 'br\} |
| 44 | .el\{\ |
| 45 | . ds -- \|\(em\| |
| 46 | . ds PI \(*p |
| 47 | . ds L" `` |
| 48 | . ds R" '' |
| 49 | 'br\} |
| 50 | .\" |
| 51 | .\" If the F register is turned on, we'll generate index entries on stderr for |
| 52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index |
| 53 | .\" entries marked with X<> in POD. Of course, you'll have to process the |
| 54 | .\" output yourself in some meaningful fashion. |
| 55 | .if \nF \{\ |
| 56 | . de IX |
| 57 | . tm Index:\\$1\t\\n%\t"\\$2" |
| 58 | .. |
| 59 | . nr % 0 |
| 60 | . rr F |
| 61 | .\} |
| 62 | .\" |
| 63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 64 | .\" way too many mistakes in technical documents. |
| 65 | .hy 0 |
| 66 | .if n .na |
| 67 | .\" |
| 68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). |
| 69 | .\" Fear. Run. Save yourself. No user-serviceable parts. |
| 70 | . \" fudge factors for nroff and troff |
| 71 | .if n \{\ |
| 72 | . ds #H 0 |
| 73 | . ds #V .8m |
| 74 | . ds #F .3m |
| 75 | . ds #[ \f1 |
| 76 | . ds #] \fP |
| 77 | .\} |
| 78 | .if t \{\ |
| 79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) |
| 80 | . ds #V .6m |
| 81 | . ds #F 0 |
| 82 | . ds #[ \& |
| 83 | . ds #] \& |
| 84 | .\} |
| 85 | . \" simple accents for nroff and troff |
| 86 | .if n \{\ |
| 87 | . ds ' \& |
| 88 | . ds ` \& |
| 89 | . ds ^ \& |
| 90 | . ds , \& |
| 91 | . ds ~ ~ |
| 92 | . ds / |
| 93 | .\} |
| 94 | .if t \{\ |
| 95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" |
| 96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' |
| 97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' |
| 98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' |
| 99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' |
| 100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' |
| 101 | .\} |
| 102 | . \" troff and (daisy-wheel) nroff accents |
| 103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' |
| 104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' |
| 105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] |
| 106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' |
| 107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' |
| 108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] |
| 109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] |
| 110 | .ds ae a\h'-(\w'a'u*4/10)'e |
| 111 | .ds Ae A\h'-(\w'A'u*4/10)'E |
| 112 | . \" corrections for vroff |
| 113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' |
| 114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' |
| 115 | . \" for low resolution devices (crt and lpr) |
| 116 | .if \n(.H>23 .if \n(.V>19 \ |
| 117 | \{\ |
| 118 | . ds : e |
| 119 | . ds 8 ss |
| 120 | . ds o a |
| 121 | . ds d- d\h'-1'\(ga |
| 122 | . ds D- D\h'-1'\(hy |
| 123 | . ds th \o'bp' |
| 124 | . ds Th \o'LP' |
| 125 | . ds ae ae |
| 126 | . ds Ae AE |
| 127 | .\} |
| 128 | .rm #[ #] #H #V #F C |
| 129 | .\" ======================================================================== |
| 130 | .\" |
| 131 | .IX Title "threads 3" |
| 132 | .TH threads 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" |
| 133 | .SH "NAME" |
| 134 | threads \- Perl extension allowing use of interpreter based threads from perl |
| 135 | .SH "SYNOPSIS" |
| 136 | .IX Header "SYNOPSIS" |
| 137 | .Vb 1 |
| 138 | \& use threads; |
| 139 | .Ve |
| 140 | .PP |
| 141 | .Vb 3 |
| 142 | \& sub start_thread { |
| 143 | \& print "Thread started\en"; |
| 144 | \& } |
| 145 | .Ve |
| 146 | .PP |
| 147 | .Vb 3 |
| 148 | \& my $thread = threads->create("start_thread","argument"); |
| 149 | \& my $thread2 = $thread->create(sub { print "I am a thread"},"argument"); |
| 150 | \& my $thread3 = async { foreach (@files) { ... } }; |
| 151 | .Ve |
| 152 | .PP |
| 153 | .Vb 2 |
| 154 | \& $thread->join(); |
| 155 | \& $thread->detach(); |
| 156 | .Ve |
| 157 | .PP |
| 158 | .Vb 2 |
| 159 | \& $thread = threads->self(); |
| 160 | \& $thread = threads->object( $tid ); |
| 161 | .Ve |
| 162 | .PP |
| 163 | .Vb 3 |
| 164 | \& $thread->tid(); |
| 165 | \& threads->tid(); |
| 166 | \& threads->self->tid(); |
| 167 | .Ve |
| 168 | .PP |
| 169 | .Vb 1 |
| 170 | \& threads->yield(); |
| 171 | .Ve |
| 172 | .PP |
| 173 | .Vb 1 |
| 174 | \& threads->list(); |
| 175 | .Ve |
| 176 | .SH "DESCRIPTION" |
| 177 | .IX Header "DESCRIPTION" |
| 178 | Perl 5.6 introduced something called interpreter threads. Interpreter |
| 179 | threads are different from \*(L"5005threads\*(R" (the thread model of Perl |
| 180 | 5.005) by creating a new perl interpreter per thread and not sharing |
| 181 | any data or state between threads by default. |
| 182 | .PP |
| 183 | Prior to perl 5.8 this has only been available to people embedding |
| 184 | perl and for emulating \fIfork()\fR on windows. |
| 185 | .PP |
| 186 | The threads \s-1API\s0 is loosely based on the old Thread.pm \s-1API\s0. It is very |
| 187 | important to note that variables are not shared between threads, all |
| 188 | variables are per default thread local. To use shared variables one |
| 189 | must use threads::shared. |
| 190 | .PP |
| 191 | It is also important to note that you must enable threads by doing |
| 192 | \&\f(CW\*(C`use threads\*(C'\fR as early as possible in the script itself and that it |
| 193 | is not possible to enable threading inside an \f(CW\*(C`eval ""\*(C'\fR, \f(CW\*(C`do\*(C'\fR, |
| 194 | \&\f(CW\*(C`require\*(C'\fR, or \f(CW\*(C`use\*(C'\fR. In particular, if you are intending to share |
| 195 | variables with threads::shared, you must \f(CW\*(C`use threads\*(C'\fR before you |
| 196 | \&\f(CW\*(C`use threads::shared\*(C'\fR and \f(CW\*(C`threads\*(C'\fR will emit a warning if you do |
| 197 | it the other way around. |
| 198 | .IP "$thread = threads\->create(function, \s-1LIST\s0)" 4 |
| 199 | .IX Item "$thread = threads->create(function, LIST)" |
| 200 | This will create a new thread with the entry point function and give |
| 201 | it \s-1LIST\s0 as parameters. It will return the corresponding threads |
| 202 | object, or \f(CW\*(C`undef\*(C'\fR if thread creation failed. The \fInew()\fR method is an |
| 203 | alias for \fIcreate()\fR. |
| 204 | .IP "$thread\->join" 4 |
| 205 | .IX Item "$thread->join" |
| 206 | This will wait for the corresponding thread to join. When the thread |
| 207 | finishes, \fIjoin()\fR will return the return values of the entry point |
| 208 | function. If the thread has been detached, an error will be thrown. |
| 209 | .Sp |
| 210 | The context (void, scalar or list) of the thread creation is also the |
| 211 | context for \fIjoin()\fR. This means that if you intend to return an array |
| 212 | from a thread, you must use \f(CW\*(C`my ($thread) = threads\-\*(C'\fRnew(...)>, and |
| 213 | that if you intend to return a scalar, you must use \f(CW\*(C`my $thread = ...\*(C'\fR. |
| 214 | .Sp |
| 215 | If the program exits without all other threads having been either |
| 216 | joined or detached, then a warning will be issued. (A program exits |
| 217 | either because one of its threads explicitly calls \fIexit()\fR, or in the |
| 218 | case of the main thread, reaches the end of the main program file.) |
| 219 | .IP "$thread\->detach" 4 |
| 220 | .IX Item "$thread->detach" |
| 221 | Will make the thread unjoinable, and cause any eventual return value |
| 222 | to be discarded. |
| 223 | .IP "threads\->self" 4 |
| 224 | .IX Item "threads->self" |
| 225 | This will return the thread object for the current thread. |
| 226 | .IP "$thread\->tid" 4 |
| 227 | .IX Item "$thread->tid" |
| 228 | This will return the id of the thread. Thread IDs are integers, with |
| 229 | the main thread in a program being 0. Currently Perl assigns a unique |
| 230 | tid to every thread ever created in your program, assigning the first |
| 231 | thread to be created a tid of 1, and increasing the tid by 1 for each |
| 232 | new thread that's created. |
| 233 | .Sp |
| 234 | \&\s-1NB\s0 the class method \f(CW\*(C`threads\->tid()\*(C'\fR is a quick way to get the |
| 235 | current thread id if you don't have your thread object handy. |
| 236 | .IP "threads\->object( tid )" 4 |
| 237 | .IX Item "threads->object( tid )" |
| 238 | This will return the thread object for the thread associated with the |
| 239 | specified tid. Returns undef if there is no thread associated with the tid |
| 240 | or no tid is specified or the specified tid is undef. |
| 241 | .IP "threads\->\fIyield()\fR;" 4 |
| 242 | .IX Item "threads->yield();" |
| 243 | This is a suggestion to the \s-1OS\s0 to let this thread yield \s-1CPU\s0 time to other |
| 244 | threads. What actually happens is highly dependent upon the underlying |
| 245 | thread implementation. |
| 246 | .Sp |
| 247 | You may do \f(CW\*(C`use threads qw(yield)\*(C'\fR then use just a bare \f(CW\*(C`yield\*(C'\fR in your |
| 248 | code. |
| 249 | .IP "threads\->\fIlist()\fR;" 4 |
| 250 | .IX Item "threads->list();" |
| 251 | This will return a list of all non joined, non detached threads. |
| 252 | .IP "async \s-1BLOCK\s0;" 4 |
| 253 | .IX Item "async BLOCK;" |
| 254 | \&\f(CW\*(C`async\*(C'\fR creates a thread to execute the block immediately following |
| 255 | it. This block is treated as an anonymous sub, and so must have a |
| 256 | semi-colon after the closing brace. Like \f(CW\*(C`threads\->new\*(C'\fR, \f(CW\*(C`async\*(C'\fR |
| 257 | returns a thread object. |
| 258 | .SH "WARNINGS" |
| 259 | .IX Header "WARNINGS" |
| 260 | .ie n .IP "A thread exited while %d other threads were still running" 4 |
| 261 | .el .IP "A thread exited while \f(CW%d\fR other threads were still running" 4 |
| 262 | .IX Item "A thread exited while %d other threads were still running" |
| 263 | A thread (not necessarily the main thread) exited while there were |
| 264 | still other threads running. Usually it's a good idea to first collect |
| 265 | the return values of the created threads by joining them, and only then |
| 266 | exit from the main thread. |
| 267 | .SH "TODO" |
| 268 | .IX Header "TODO" |
| 269 | The current implementation of threads has been an attempt to get |
| 270 | a correct threading system working that could be built on, |
| 271 | and optimized, in newer versions of perl. |
| 272 | .PP |
| 273 | Currently the overhead of creating a thread is rather large, |
| 274 | also the cost of returning values can be large. These are areas |
| 275 | were there most likely will be work done to optimize what data |
| 276 | that needs to be cloned. |
| 277 | .SH "BUGS" |
| 278 | .IX Header "BUGS" |
| 279 | .IP "Parent-Child threads." 4 |
| 280 | .IX Item "Parent-Child threads." |
| 281 | On some platforms it might not be possible to destroy \*(L"parent\*(R" |
| 282 | threads while there are still existing child \*(L"threads\*(R". |
| 283 | .Sp |
| 284 | This will possibly be fixed in later versions of perl. |
| 285 | .IP "tid is I32" 4 |
| 286 | .IX Item "tid is I32" |
| 287 | The thread id is a 32 bit integer, it can potentially overflow. |
| 288 | This might be fixed in a later version of perl. |
| 289 | .IP "Returning objects" 4 |
| 290 | .IX Item "Returning objects" |
| 291 | When you return an object the entire stash that the object is blessed |
| 292 | as well. This will lead to a large memory usage. The ideal situation |
| 293 | would be to detect the original stash if it existed. |
| 294 | .IP "Creating threads inside \s-1BEGIN\s0 blocks" 4 |
| 295 | .IX Item "Creating threads inside BEGIN blocks" |
| 296 | Creating threads inside \s-1BEGIN\s0 blocks (or during the compilation phase |
| 297 | in general) does not work. (In Windows, trying to use \fIfork()\fR inside |
| 298 | \&\s-1BEGIN\s0 blocks is an equally losing proposition, since it has been |
| 299 | implemented in very much the same way as threads.) |
| 300 | .IP "\s-1PERL_OLD_SIGNALS\s0 are not threadsafe, will not be." 4 |
| 301 | .IX Item "PERL_OLD_SIGNALS are not threadsafe, will not be." |
| 302 | If your Perl has been built with \s-1PERL_OLD_SIGNALS\s0 (one has |
| 303 | to explicitly add that symbol to ccflags, see \f(CW\*(C`perl \-V\*(C'\fR), |
| 304 | signal handling is not threadsafe. |
| 305 | .SH "AUTHOR and COPYRIGHT" |
| 306 | .IX Header "AUTHOR and COPYRIGHT" |
| 307 | Arthur Bergman <sky at nanisky.com> |
| 308 | .PP |
| 309 | threads is released under the same license as Perl. |
| 310 | .PP |
| 311 | Thanks to |
| 312 | .PP |
| 313 | Richard Soderberg <perl at crystalflame.net> |
| 314 | Helping me out tons, trying to find reasons for races and other weird bugs! |
| 315 | .PP |
| 316 | Simon Cozens <simon at brecon.co.uk> |
| 317 | Being there to answer zillions of annoying questions |
| 318 | .PP |
| 319 | Rocco Caputo <troc at netrus.net> |
| 320 | .PP |
| 321 | Vipul Ved Prakash <mail at vipul.net> |
| 322 | Helping with debugging. |
| 323 | .PP |
| 324 | please join perl\-ithreads@perl.org for more information |
| 325 | .SH "SEE ALSO" |
| 326 | .IX Header "SEE ALSO" |
| 327 | threads::shared, perlthrtut, |
| 328 | <http://www.perl.com/pub/a/2002/06/11/threads.html>, |
| 329 | perlcall, perlembed, perlguts |