Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / perl-5.8.0 / man / man3 / threads.3
CommitLineData
86530b38
AT
1.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
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 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide"
133.SH "NAME"
134threads \- 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"
178Perl 5.6 introduced something called interpreter threads. Interpreter
179threads are different from \*(L"5005threads\*(R" (the thread model of Perl
1805.005) by creating a new perl interpreter per thread and not sharing
181any data or state between threads by default.
182.PP
183Prior to perl 5.8 this has only been available to people embedding
184perl and for emulating \fIfork()\fR on windows.
185.PP
186The threads \s-1API\s0 is loosely based on the old Thread.pm \s-1API\s0. It is very
187important to note that variables are not shared between threads, all
188variables are per default thread local. To use shared variables one
189must use threads::shared.
190.PP
191It 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
193is 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
195variables 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
197it the other way around.
198.IP "$thread = threads\->create(function, \s-1LIST\s0)" 4
199.IX Item "$thread = threads->create(function, LIST)"
200This will create a new thread with the entry point function and give
201it \s-1LIST\s0 as parameters. It will return the corresponding threads
202object. The \fInew()\fR method is an alias for \fIcreate()\fR.
203.IP "$thread\->join" 4
204.IX Item "$thread->join"
205This will wait for the corresponding thread to join. When the thread
206finishes, \fIjoin()\fR will return the return values of the entry point
207function. If the thread has been detached, an error will be thrown.
208If the program exits without all other threads having been either
209joined or detached, then a warning will be issued. (A program exits
210either because one of its threads explicitly calls \fIexit()\fR, or in the
211case of the main thread, reaches the end of the main program file.)
212.IP "$thread\->detach" 4
213.IX Item "$thread->detach"
214Will make the thread unjoinable, and cause any eventual return value
215to be discarded.
216.IP "threads\->self" 4
217.IX Item "threads->self"
218This will return the thread object for the current thread.
219.IP "$thread\->tid" 4
220.IX Item "$thread->tid"
221This will return the id of the thread. Thread IDs are integers, with
222the main thread in a program being 0. Currently Perl assigns a unique
223tid to every thread ever created in your program, assigning the first
224thread to be created a tid of 1, and increasing the tid by 1 for each
225new thread that's created.
226.Sp
227\&\s-1NB\s0 the class method \f(CW\*(C`threads\->tid()\*(C'\fR is a quick way to get the
228current thread id if you don't have your thread object handy.
229.IP "threads\->object( tid )" 4
230.IX Item "threads->object( tid )"
231This will return the thread object for the thread associated with the
232specified tid. Returns undef if there is no thread associated with the tid
233or no tid is specified or the specified tid is undef.
234.IP "threads\->\fIyield()\fR;" 4
235.IX Item "threads->yield();"
236This is a suggestion to the \s-1OS\s0 to let this thread yield \s-1CPU\s0 time to other
237threads. What actually happens is highly dependent upon the underlying
238thread implementation.
239.Sp
240You may do \f(CW\*(C`use threads qw(yield)\*(C'\fR then use just a bare \f(CW\*(C`yield\*(C'\fR in your
241code.
242.IP "threads\->\fIlist()\fR;" 4
243.IX Item "threads->list();"
244This will return a list of all non joined, non detached threads.
245.IP "async \s-1BLOCK\s0;" 4
246.IX Item "async BLOCK;"
247\&\f(CW\*(C`async\*(C'\fR creates a thread to execute the block immediately following
248it. This block is treated as an anonymous sub, and so must have a
249semi-colon after the closing brace. Like \f(CW\*(C`threads\->new\*(C'\fR, \f(CW\*(C`async\*(C'\fR
250returns a thread object.
251.SH "WARNINGS"
252.IX Header "WARNINGS"
253.ie n .IP "A thread exited while %d other threads were still running" 4
254.el .IP "A thread exited while \f(CW%d\fR other threads were still running" 4
255.IX Item "A thread exited while %d other threads were still running"
256A thread (not necessarily the main thread) exited while there were
257still other threads running. Usually it's a good idea to first collect
258the return values of the created threads by joining them, and only then
259exit from the main thread.
260.SH "TODO"
261.IX Header "TODO"
262The current implementation of threads has been an attempt to get
263a correct threading system working that could be built on,
264and optimized, in newer versions of perl.
265.PP
266Currently the overhead of creating a thread is rather large,
267also the cost of returning values can be large. These are areas
268were there most likely will be work done to optimize what data
269that needs to be cloned.
270.SH "BUGS"
271.IX Header "BUGS"
272.IP "Parent-Child threads." 4
273.IX Item "Parent-Child threads."
274On some platforms it might not be possible to destroy \*(L"parent\*(R"
275threads while there are still existing child \*(L"threads\*(R".
276.Sp
277This will possibly be fixed in later versions of perl.
278.IP "tid is I32" 4
279.IX Item "tid is I32"
280The thread id is a 32 bit integer, it can potentially overflow.
281This might be fixed in a later version of perl.
282.IP "Returning objects" 4
283.IX Item "Returning objects"
284When you return an object the entire stash that the object is blessed
285as well. This will lead to a large memory usage. The ideal situation
286would be to detect the original stash if it existed.
287.IP "Creating threads inside \s-1BEGIN\s0 blocks" 4
288.IX Item "Creating threads inside BEGIN blocks"
289Creating threads inside \s-1BEGIN\s0 blocks (or during the compilation phase
290in general) does not work. (In Windows, trying to use \fIfork()\fR inside
291\&\s-1BEGIN\s0 blocks is an equally losing proposition, since it has been
292implemented in very much the same way as threads.)
293.IP "\s-1PERL_OLD_SIGNALS\s0 are not threadsafe, will not be." 4
294.IX Item "PERL_OLD_SIGNALS are not threadsafe, will not be."
295If your Perl has been built with \s-1PERL_OLD_SIGNALS\s0 (one has
296to explicitly add that symbol to ccflags, see \f(CW\*(C`perl \-V\*(C'\fR),
297signal handling is not threadsafe.
298.SH "AUTHOR and COPYRIGHT"
299.IX Header "AUTHOR and COPYRIGHT"
300Arthur Bergman <arthur at contiller.se>
301.PP
302threads is released under the same license as Perl.
303.PP
304Thanks to
305.PP
306Richard Soderberg <rs at crystalflame.net>
307Helping me out tons, trying to find reasons for races and other weird bugs!
308.PP
309Simon Cozens <simon at brecon.co.uk>
310Being there to answer zillions of annoying questions
311.PP
312Rocco Caputo <troc at netrus.net>
313.PP
314Vipul Ved Prakash <mail at vipul.net>
315Helping with debugging.
316.PP
317please join perl\-ithreads@perl.org for more information
318.SH "SEE ALSO"
319.IX Header "SEE ALSO"
320threads::shared, perlthrtut,
321<http://www.perl.com/pub/a/2002/06/11/threads.html>,
322perlcall, perlembed, perlguts