Commit | Line | Data |
---|---|---|
920dae64 AT |
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 "Thread 3" | |
132 | .TH Thread 3 "2001-09-21" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Thread \- manipulate threads in Perl (for old code only) | |
135 | .SH "CAVEAT" | |
136 | .IX Header "CAVEAT" | |
137 | Perl has two thread models. | |
138 | .PP | |
139 | In Perl 5.005 the thread model was that all data is implicitly shared | |
140 | and shared access to data has to be explicitly synchronized. | |
141 | This model is called \*(L"5005threads\*(R". | |
142 | .PP | |
143 | In Perl 5.6 a new model was introduced in which all is was thread | |
144 | local and shared access to data has to be explicitly declared. | |
145 | This model is called \*(L"ithreads\*(R", for \*(L"interpreter threads\*(R". | |
146 | .PP | |
147 | In Perl 5.6 the ithreads model was not available as a public \s-1API\s0, | |
148 | only as an internal \s-1API\s0 that was available for extension writers, | |
149 | and to implement \fIfork()\fR emulation on Win32 platforms. | |
150 | .PP | |
151 | In Perl 5.8 the ithreads model became available through the \f(CW\*(C`threads\*(C'\fR | |
152 | module. | |
153 | .PP | |
154 | Neither model is configured by default into Perl (except, as mentioned | |
155 | above, in Win32 ithreads are always available.) You can see your | |
156 | Perl's threading configuration by running \f(CW\*(C`perl \-V\*(C'\fR and looking for | |
157 | the \fIuse...threads\fR variables, or inside script by \f(CW\*(C`use Config;\*(C'\fR | |
158 | and testing for \f(CW$Config{use5005threads}\fR and \f(CW$Config{useithreads}\fR. | |
159 | .PP | |
160 | For old code and interim backwards compatibility, the Thread module | |
161 | has been reworked to function as a frontend for both 5005threads and | |
162 | ithreads. | |
163 | .PP | |
164 | Note that the compatibility is not complete: because the data sharing | |
165 | models are directly opposed, anything to do with data sharing has to | |
166 | be thought differently. With the ithreads you must explicitly \fIshare()\fR | |
167 | variables between the threads. | |
168 | .PP | |
169 | For new code the use of the \f(CW\*(C`Thread\*(C'\fR module is discouraged and | |
170 | the direct use of the \f(CW\*(C`threads\*(C'\fR and \f(CW\*(C`threads::shared\*(C'\fR modules | |
171 | is encouraged instead. | |
172 | .PP | |
173 | Finally, note that there are many known serious problems with the | |
174 | 5005threads, one of the least of which is that regular expression | |
175 | match variables like \f(CW$1\fR are not threadsafe, that is, they easily get | |
176 | corrupted by competing threads. Other problems include more insidious | |
177 | data corruption and mysterious crashes. You are seriously urged to | |
178 | use ithreads instead. | |
179 | .SH "SYNOPSIS" | |
180 | .IX Header "SYNOPSIS" | |
181 | .Vb 1 | |
182 | \& use Thread; | |
183 | .Ve | |
184 | .PP | |
185 | .Vb 1 | |
186 | \& my $t = Thread->new(\e&start_sub, @start_args); | |
187 | .Ve | |
188 | .PP | |
189 | .Vb 3 | |
190 | \& $result = $t->join; | |
191 | \& $result = $t->eval; | |
192 | \& $t->detach; | |
193 | .Ve | |
194 | .PP | |
195 | .Vb 3 | |
196 | \& if ($t->done) { | |
197 | \& $t->join; | |
198 | \& } | |
199 | .Ve | |
200 | .PP | |
201 | .Vb 3 | |
202 | \& if($t->equal($another_thread)) { | |
203 | \& # ... | |
204 | \& } | |
205 | .Ve | |
206 | .PP | |
207 | .Vb 1 | |
208 | \& yield(); | |
209 | .Ve | |
210 | .PP | |
211 | .Vb 1 | |
212 | \& my $tid = Thread->self->tid; | |
213 | .Ve | |
214 | .PP | |
215 | .Vb 3 | |
216 | \& lock($scalar); | |
217 | \& lock(@array); | |
218 | \& lock(%hash); | |
219 | .Ve | |
220 | .PP | |
221 | .Vb 1 | |
222 | \& lock(\e&sub); # not available with ithreads | |
223 | .Ve | |
224 | .PP | |
225 | .Vb 1 | |
226 | \& $flags = $t->flags; # not available with ithreads | |
227 | .Ve | |
228 | .PP | |
229 | .Vb 1 | |
230 | \& my @list = Thread->list; # not available with ithreads | |
231 | .Ve | |
232 | .PP | |
233 | .Vb 1 | |
234 | \& use Thread 'async'; | |
235 | .Ve | |
236 | .SH "DESCRIPTION" | |
237 | .IX Header "DESCRIPTION" | |
238 | The \f(CW\*(C`Thread\*(C'\fR module provides multithreading support for perl. | |
239 | .SH "FUNCTIONS" | |
240 | .IX Header "FUNCTIONS" | |
241 | .IP "$thread = Thread\->new(\e&start_sub)" 8 | |
242 | .IX Item "$thread = Thread->new(&start_sub)" | |
243 | .PD 0 | |
244 | .IP "$thread = Thread\->new(\e&start_sub, \s-1LIST\s0)" 8 | |
245 | .IX Item "$thread = Thread->new(&start_sub, LIST)" | |
246 | .PD | |
247 | \&\f(CW\*(C`new\*(C'\fR starts a new thread of execution in the referenced subroutine. The | |
248 | optional list is passed as parameters to the subroutine. Execution | |
249 | continues in both the subroutine and the code after the \f(CW\*(C`new\*(C'\fR call. | |
250 | .Sp | |
251 | \&\f(CW\*(C`Thread\->new\*(C'\fR returns a thread object representing the newly created | |
252 | thread. | |
253 | .IP "lock \s-1VARIABLE\s0" 8 | |
254 | .IX Item "lock VARIABLE" | |
255 | \&\f(CW\*(C`lock\*(C'\fR places a lock on a variable until the lock goes out of scope. | |
256 | .Sp | |
257 | If the variable is locked by another thread, the \f(CW\*(C`lock\*(C'\fR call will | |
258 | block until it's available. \f(CW\*(C`lock\*(C'\fR is recursive, so multiple calls | |
259 | to \f(CW\*(C`lock\*(C'\fR are safe\*(--the variable will remain locked until the | |
260 | outermost lock on the variable goes out of scope. | |
261 | .Sp | |
262 | Locks on variables only affect \f(CW\*(C`lock\*(C'\fR calls\*(--they do \fInot\fR affect normal | |
263 | access to a variable. (Locks on subs are different, and covered in a bit.) | |
264 | If you really, \fIreally\fR want locks to block access, then go ahead and tie | |
265 | them to something and manage this yourself. This is done on purpose. | |
266 | While managing access to variables is a good thing, Perl doesn't force | |
267 | you out of its living room... | |
268 | .Sp | |
269 | If a container object, such as a hash or array, is locked, all the | |
270 | elements of that container are not locked. For example, if a thread | |
271 | does a \f(CW\*(C`lock @a\*(C'\fR, any other thread doing a \f(CW\*(C`lock($a[12])\*(C'\fR won't | |
272 | block. | |
273 | .Sp | |
274 | With 5005threads you may also \f(CW\*(C`lock\*(C'\fR a sub, using \f(CW\*(C`lock &sub\*(C'\fR. | |
275 | Any calls to that sub from another thread will block until the lock | |
276 | is released. This behaviour is not equivalent to declaring the sub | |
277 | with the \f(CW\*(C`locked\*(C'\fR attribute. The \f(CW\*(C`locked\*(C'\fR attribute serializes | |
278 | access to a subroutine, but allows different threads non-simultaneous | |
279 | access. \f(CW\*(C`lock &sub\*(C'\fR, on the other hand, will not allow \fIany\fR other | |
280 | thread access for the duration of the lock. | |
281 | .Sp | |
282 | Finally, \f(CW\*(C`lock\*(C'\fR will traverse up references exactly \fIone\fR level. | |
283 | \&\f(CW\*(C`lock(\e$a)\*(C'\fR is equivalent to \f(CW\*(C`lock($a)\*(C'\fR, while \f(CW\*(C`lock(\e\e$a)\*(C'\fR is not. | |
284 | .IP "async \s-1BLOCK\s0;" 8 | |
285 | .IX Item "async BLOCK;" | |
286 | \&\f(CW\*(C`async\*(C'\fR creates a thread to execute the block immediately following | |
287 | it. This block is treated as an anonymous sub, and so must have a | |
288 | semi-colon after the closing brace. Like \f(CW\*(C`Thread\->new\*(C'\fR, \f(CW\*(C`async\*(C'\fR | |
289 | returns a thread object. | |
290 | .IP "Thread\->self" 8 | |
291 | .IX Item "Thread->self" | |
292 | The \f(CW\*(C`Thread\->self\*(C'\fR function returns a thread object that represents | |
293 | the thread making the \f(CW\*(C`Thread\->self\*(C'\fR call. | |
294 | .IP "cond_wait \s-1VARIABLE\s0" 8 | |
295 | .IX Item "cond_wait VARIABLE" | |
296 | The \f(CW\*(C`cond_wait\*(C'\fR function takes a \fBlocked\fR variable as | |
297 | a parameter, unlocks the variable, and blocks until another thread | |
298 | does a \f(CW\*(C`cond_signal\*(C'\fR or \f(CW\*(C`cond_broadcast\*(C'\fR for that same locked | |
299 | variable. The variable that \f(CW\*(C`cond_wait\*(C'\fR blocked on is relocked | |
300 | after the \f(CW\*(C`cond_wait\*(C'\fR is satisfied. If there are multiple threads | |
301 | \&\f(CW\*(C`cond_wait\*(C'\fRing on the same variable, all but one will reblock waiting | |
302 | to reaquire the lock on the variable. (So if you're only using | |
303 | \&\f(CW\*(C`cond_wait\*(C'\fR for synchronization, give up the lock as soon as | |
304 | possible.) | |
305 | .IP "cond_signal \s-1VARIABLE\s0" 8 | |
306 | .IX Item "cond_signal VARIABLE" | |
307 | The \f(CW\*(C`cond_signal\*(C'\fR function takes a locked variable as a parameter and | |
308 | unblocks one thread that's \f(CW\*(C`cond_wait\*(C'\fRing on that variable. If more than | |
309 | one thread is blocked in a \f(CW\*(C`cond_wait\*(C'\fR on that variable, only one (and | |
310 | which one is indeterminate) will be unblocked. | |
311 | .Sp | |
312 | If there are no threads blocked in a \f(CW\*(C`cond_wait\*(C'\fR on the variable, | |
313 | the signal is discarded. | |
314 | .IP "cond_broadcast \s-1VARIABLE\s0" 8 | |
315 | .IX Item "cond_broadcast VARIABLE" | |
316 | The \f(CW\*(C`cond_broadcast\*(C'\fR function works similarly to \f(CW\*(C`cond_signal\*(C'\fR. | |
317 | \&\f(CW\*(C`cond_broadcast\*(C'\fR, though, will unblock \fBall\fR the threads that are | |
318 | blocked in a \f(CW\*(C`cond_wait\*(C'\fR on the locked variable, rather than only | |
319 | one. | |
320 | .IP "yield" 8 | |
321 | .IX Item "yield" | |
322 | The \f(CW\*(C`yield\*(C'\fR function allows another thread to take control of the | |
323 | \&\s-1CPU\s0. The exact results are implementation\-dependent. | |
324 | .SH "METHODS" | |
325 | .IX Header "METHODS" | |
326 | .IP "join" 8 | |
327 | .IX Item "join" | |
328 | \&\f(CW\*(C`join\*(C'\fR waits for a thread to end and returns any values the thread | |
329 | exited with. \f(CW\*(C`join\*(C'\fR will block until the thread has ended, though | |
330 | it won't block if the thread has already terminated. | |
331 | .Sp | |
332 | If the thread being \f(CW\*(C`join\*(C'\fRed \f(CW\*(C`die\*(C'\fRd, the error it died with will | |
333 | be returned at this time. If you don't want the thread performing | |
334 | the \f(CW\*(C`join\*(C'\fR to die as well, you should either wrap the \f(CW\*(C`join\*(C'\fR in | |
335 | an \f(CW\*(C`eval\*(C'\fR or use the \f(CW\*(C`eval\*(C'\fR thread method instead of \f(CW\*(C`join\*(C'\fR. | |
336 | .IP "eval" 8 | |
337 | .IX Item "eval" | |
338 | The \f(CW\*(C`eval\*(C'\fR method wraps an \f(CW\*(C`eval\*(C'\fR around a \f(CW\*(C`join\*(C'\fR, and so waits for | |
339 | a thread to exit, passing along any values the thread might have returned. | |
340 | Errors, of course, get placed into \f(CW$@\fR. (Not available with ithreads.) | |
341 | .IP "detach" 8 | |
342 | .IX Item "detach" | |
343 | \&\f(CW\*(C`detach\*(C'\fR tells a thread that it is never going to be joined i.e. | |
344 | that all traces of its existence can be removed once it stops running. | |
345 | Errors in detached threads will not be visible anywhere \- if you want | |
346 | to catch them, you should use \f(CW$SIG\fR{_\|_DIE_\|_} or something like that. | |
347 | .IP "equal" 8 | |
348 | .IX Item "equal" | |
349 | \&\f(CW\*(C`equal\*(C'\fR tests whether two thread objects represent the same thread and | |
350 | returns true if they do. | |
351 | .IP "tid" 8 | |
352 | .IX Item "tid" | |
353 | The \f(CW\*(C`tid\*(C'\fR method returns the tid of a thread. The tid is | |
354 | a monotonically increasing integer assigned when a thread is | |
355 | created. The main thread of a program will have a tid of zero, | |
356 | while subsequent threads will have tids assigned starting with one. | |
357 | .IP "flags" 8 | |
358 | .IX Item "flags" | |
359 | The \f(CW\*(C`flags\*(C'\fR method returns the flags for the thread. This is the | |
360 | integer value corresponding to the internal flags for the thread, | |
361 | and the value may not be all that meaningful to you. | |
362 | (Not available with ithreads.) | |
363 | .IP "done" 8 | |
364 | .IX Item "done" | |
365 | The \f(CW\*(C`done\*(C'\fR method returns true if the thread you're checking has | |
366 | finished, and false otherwise. (Not available with ithreads.) | |
367 | .SH "LIMITATIONS" | |
368 | .IX Header "LIMITATIONS" | |
369 | The sequence number used to assign tids is a simple integer, and no | |
370 | checking is done to make sure the tid isn't currently in use. If a | |
371 | program creates more than 2**32 \- 1 threads in a single run, threads | |
372 | may be assigned duplicate tids. This limitation may be lifted in | |
373 | a future version of Perl. | |
374 | .SH "SEE ALSO" | |
375 | .IX Header "SEE ALSO" | |
376 | threads::shared (not available with 5005threads) | |
377 | .PP | |
378 | attributes, Thread::Queue, Thread::Semaphore, | |
379 | Thread::Specific (not available with ithreads) |