File: threads.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (1120 lines) | stat: -rw-r--r-- 90,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>threads - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>threads</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-pragmas.html">Pragmas</a> &gt;
      
    
    threads
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>threads</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#VERSION">VERSION</a><li><a href="#WARNING">WARNING</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#EXITING-A-THREAD">EXITING A THREAD</a><li><a href="#THREAD-STATE">THREAD STATE</a><li><a href="#THREAD-CONTEXT">THREAD CONTEXT</a><ul><li><a href="#Explicit-context">Explicit context</a><li><a href="#Implicit-context">Implicit context</a><li><a href="#%24thr-%3ewantarray()">$thr->wantarray()</a><li><a href="#threads-%3ewantarray()">threads->wantarray()</a></ul><li><a href="#THREAD-STACK-SIZE">THREAD STACK SIZE</a><li><a href="#THREAD-SIGNALLING">THREAD SIGNALLING</a><li><a href="#WARNINGS">WARNINGS</a><li><a href="#ERRORS">ERRORS</a><li><a href="#BUGS-AND-LIMITATIONS">BUGS AND LIMITATIONS</a><li><a href="#REQUIREMENTS">REQUIREMENTS</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#LICENSE">LICENSE</a><li><a href="#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>threads - Perl interpreter-based threads</p>
<a name="VERSION"></a><h1>VERSION</h1>
<p>This document describes threads version 2.15</p>
<a name="WARNING"></a><h1>WARNING</h1>
<p>The "interpreter-based threads" provided by Perl are not the fast, lightweight
system for multitasking that one might expect or hope for.  Threads are
implemented in a way that make them easy to misuse.  Few people know how to
use them correctly or will be able to provide help.</p>
<p>The use of interpreter-based threads in perl is officially
<a href="perlpolicy.html#discouraged">discouraged</a>.</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span> <span class="s">(</span><span class="q">&#39;yield&#39;</span><span class="cm">,</span></li><li>                 <span class="q">&#39;stack_size&#39;</span> <span class="cm">=&gt;</span> <span class="n">64</span>*<span class="n">4096</span><span class="cm">,</span></li><li>                 <span class="q">&#39;exit&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;threads_only&#39;</span><span class="cm">,</span></li><li>                 <span class="q">&#39;stringify&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="start_thread"></a>    sub <span class="m">start_thread</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">@args</span> = <span class="i">@_</span><span class="sc">;</span></li><li>        <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">&#39;Thread started: &#39;</span><span class="cm">,</span> <a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">&#39; &#39;</span><span class="cm">,</span> <span class="i">@args</span><span class="s">)</span><span class="cm">,</span> <span class="q">&quot;\n&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="q">&#39;start_thread&#39;</span><span class="cm">,</span> <span class="q">&#39;argument&#39;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$thr</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">&quot;I am a thread\n&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="s">}</span><span class="s">)</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr2</span> = <span class="i">async</span> <span class="s">{</span> foreach <span class="s">(</span><span class="i">@files</span><span class="s">)</span> <span class="s">{</span> ... <span class="s">}</span> <span class="s">}</span><span class="sc">;</span></li><li>    <span class="i">$thr2</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    if <span class="s">(</span><a class="l_k" href="functions/my.html">my</a> <span class="i">$err</span> = <span class="i">$thr2</span><span class="i">-&gt;error</span><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/warn.html">warn</a><span class="s">(</span><span class="q">&quot;Thread error: $err\n&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># Invoke thread in list context (implicit) so it can return a list</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$thr</span><span class="s">)</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/return.html">return</a> <span class="s">(</span><span class="q">qw/a b c/</span><span class="s">)</span><span class="sc">;</span> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># or specify list context explicitly</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">&#39;context&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;list&#39;</span><span class="s">}</span><span class="cm">,</span></li><li>                              <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/return.html">return</a> <span class="s">(</span><span class="q">qw/a b c/</span><span class="s">)</span><span class="sc">;</span> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@results</span> = <span class="i">$thr</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="i">$thr</span><span class="i">-&gt;detach</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Get a thread&#39;s object</span></li><li>    <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;self</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;object</span><span class="s">(</span><span class="i">$tid</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Get a thread&#39;s ID</span></li><li>    <span class="i">$tid</span> = <span class="w">threads</span><span class="w">-&gt;tid</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$tid</span> = <span class="i">$thr</span><span class="i">-&gt;tid</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$tid</span> = <span class="q">&quot;$thr&quot;</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Give other threads a chance to run</span></li><li>    <span class="w">threads</span><span class="w">-&gt;yield</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">yield</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Lists of non-detached threads</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@threads</span> = <span class="w">threads</span><span class="w">-&gt;list</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thread_count</span> = <span class="w">threads</span><span class="w">-&gt;list</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@running</span> = <span class="w">threads</span><span class="w">-&gt;list</span><span class="s">(</span><span class="w">threads::running</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@joinable</span> = <span class="w">threads</span><span class="w">-&gt;list</span><span class="s">(</span><span class="w">threads::joinable</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Test thread objects</span></li><li>    if <span class="s">(</span><span class="i">$thr1</span> == <span class="i">$thr2</span><span class="s">)</span> <span class="s">{</span></li><li>        ...</li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># Manage thread stack size</span></li><li>    <span class="i">$stack_size</span> = <span class="w">threads</span><span class="w">-&gt;get_stack_size</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$old_size</span> = <span class="w">threads</span><span class="w">-&gt;set_stack_size</span><span class="s">(</span><span class="n">32</span>*<span class="n">4096</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Create a thread with a specific context and stack size</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span> <span class="q">&#39;context&#39;</span>    <span class="cm">=&gt;</span> <span class="q">&#39;list&#39;</span><span class="cm">,</span></li><li>                                <span class="q">&#39;stack_size&#39;</span> <span class="cm">=&gt;</span> <span class="n">32</span>*<span class="n">4096</span><span class="cm">,</span></li><li>                                <span class="q">&#39;exit&#39;</span>       <span class="cm">=&gt;</span> <span class="q">&#39;thread_only&#39;</span> <span class="s">}</span><span class="cm">,</span></li><li>                              \<span class="i">&amp;foo</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Get thread&#39;s context</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$wantarray</span> = <span class="i">$thr</span><span class="i">-&gt;wantarray</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Check thread&#39;s state</span></li><li>    if <span class="s">(</span><span class="i">$thr</span><span class="i">-&gt;is_running</span><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/sleep.html">sleep</a><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    if <span class="s">(</span><span class="i">$thr</span><span class="i">-&gt;is_joinable</span><span class="s">(</span><span class="s">)</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="i">$thr</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># Send a signal to a thread</span></li><li>    <span class="i">$thr</span><span class="i">-&gt;kill</span><span class="s">(</span><span class="q">&#39;SIGUSR1&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Exit a thread</span></li><li>    <span class="w">threads</span><span class="w">-&gt;exit</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>Since Perl 5.8, thread programming has been available using a model called
<i>interpreter threads</i> which provides a new Perl interpreter for each
thread, and, by default, results in no data or state information being shared
between threads.</p>
<p>(Prior to Perl 5.8, <i>5005threads</i> was available through the <code class="inline"><span class="w">Thread</span>.<span class="w">pm</span></code>
 API.
This threading model has been deprecated, and was removed as of Perl 5.10.0.)</p>
<p>As just mentioned, all variables are, by default, thread local.  To use shared
variables, you need to also load <a href="threads/shared.html">threads::shared</a>:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">threads::shared</span><span class="sc">;</span></li></ol></pre><p>When loading <a href="threads/shared.html">threads::shared</a>, you must <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span></code>
 before you
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">threads::shared</span></code>
.  (<code class="inline"><span class="w">threads</span></code>
 will emit a warning if you do it the
other way around.)</p>
<p>It is strongly recommended that you enable threads via <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span></code>
 as early
as possible in your script.</p>
<p>If needed, scripts can be written so as to run on both threaded and
non-threaded Perls:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$can_use_threads</span> = <a class="l_k" href="functions/eval.html">eval</a> <span class="q">&#39;use threads; 1&#39;</span><span class="sc">;</span></li><li>    if <span class="s">(</span><span class="i">$can_use_threads</span><span class="s">)</span> <span class="s">{</span></li><li>        <span class="c"># Do processing using threads</span></li><li>        ...</li><li>    <span class="s">}</span> else <span class="s">{</span></li><li>        <span class="c"># Do it without using threads</span></li><li>        ...</li><li>    <span class="s">}</span></li></ol></pre><ul>
<li><a name="%24thr-%3d-threads-%3ecreate(FUNCTION%2c-ARGS)"></a><b>$thr = threads-&gt;create(FUNCTION, ARGS)</b>
<p>This will create a new thread that will begin execution with the specified
entry point function, and give it the <i>ARGS</i> list as parameters.  It will
return the corresponding threads object, or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> if thread creation failed.</p>
<p><i>FUNCTION</i> may either be the name of a function, an anonymous subroutine, or
a code ref.</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="q">'func_name'</span><span class="cm">,</span> ...<span class="s">)</span><span class="sc">;</span></li><li>        <span class="c"># or</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> ... <span class="s">}</span><span class="cm">,</span> ...<span class="s">)</span><span class="sc">;</span></li><li>        <span class="c"># or</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span>\<span class="i">&amp;func</span><span class="cm">,</span> ...<span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The <code class="inline"><span class="i">-&gt;new</span><span class="s">(</span><span class="s">)</span></code>
 method is an alias for <code class="inline"><span class="i">-&gt;create</span><span class="s">(</span><span class="s">)</span></code>
.</p>
</li>
<li><a name="%24thr-%3ejoin()"></a><b>$thr-&gt;join()</b>
<p>This will wait for the corresponding thread to complete its execution.  When
the thread finishes, <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 will return the return value(s) of the
entry point function.</p>
<p>The context (void, scalar or list) for the return value(s) for <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>

is determined at the time of thread creation.</p>
<pre class="verbatim"><ol><li>    <span class="c"># Create thread in list context (implicit)</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$thr1</span><span class="s">)</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>                                    <a class="l_k" href="functions/my.html">my</a> <span class="i">@results</span> = <span class="q">qw(a b c)</span><span class="sc">;</span></li><li>                                    <a class="l_k" href="functions/return.html">return</a> <span class="s">(</span><span class="i">@results</span><span class="s">)</span><span class="sc">;</span></li><li>                                 <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c">#   or (explicit)</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr1</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">&#39;context&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;list&#39;</span><span class="s">}</span><span class="cm">,</span></li><li>                               <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>                                    <a class="l_k" href="functions/my.html">my</a> <span class="i">@results</span> = <span class="q">qw(a b c)</span><span class="sc">;</span></li><li>                                    <a class="l_k" href="functions/return.html">return</a> <span class="s">(</span><span class="i">@results</span><span class="s">)</span><span class="sc">;</span></li><li>                               <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># Retrieve list results from thread</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@res1</span> = <span class="i">$thr1</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Create thread in scalar context (implicit)</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr2</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>                                    <a class="l_k" href="functions/my.html">my</a> <span class="i">$result</span> = <span class="n">42</span><span class="sc">;</span></li><li>                                    <a class="l_k" href="functions/return.html">return</a> <span class="s">(</span><span class="i">$result</span><span class="s">)</span><span class="sc">;</span></li><li>                                 <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># Retrieve scalar result from thread</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$res2</span> = <span class="i">$thr2</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Create a thread in void context (explicit)</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr3</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">&#39;void&#39;</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="s">}</span><span class="cm">,</span></li><li>                               <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">&quot;Hello, world\n&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># Join the thread in void context (i.e., no return value)</span></li><li>    <span class="i">$thr3</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>See <a href="#THREAD-CONTEXT">THREAD CONTEXT</a> for more details.</p>
<p>If the program exits without all threads having either been joined or
detached, then a warning will be issued.</p>
<p>Calling <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 or <code class="inline"><span class="i">-&gt;detach</span><span class="s">(</span><span class="s">)</span></code>
 on an already joined thread will
cause an error to be thrown.</p>
</li>
<li><a name="%24thr-%3edetach()"></a><b>$thr-&gt;detach()</b>
<p>Makes the thread unjoinable, and causes any eventual return value to be
discarded.  When the program exits, any detached threads that are still
running are silently terminated.</p>
<p>If the program exits without all threads having either been joined or
detached, then a warning will be issued.</p>
<p>Calling <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 or <code class="inline"><span class="i">-&gt;detach</span><span class="s">(</span><span class="s">)</span></code>
 on an already detached thread
will cause an error to be thrown.</p>
</li>
<li><a name="threads-%3edetach()"></a><b>threads-&gt;detach()</b>
<p>Class method that allows a thread to detach itself.</p>
</li>
<li><a name="threads-%3eself()"></a><b>threads-&gt;self()</b>
<p>Class method that allows a thread to obtain its own <i>threads</i> object.</p>
</li>
<li><a name="%24thr-%3etid()"></a><b>$thr-&gt;tid()</b>
<p>Returns the ID of the thread.  Thread IDs are unique integers with the main
thread in a program being 0, and incrementing by 1 for every thread created.</p>
</li>
<li><a name="threads-%3etid()"></a><b>threads-&gt;tid()</b>
<p>Class method that allows a thread to obtain its own ID.</p>
</li>
<li><a name="%22%24thr%22"></a><b>"$thr"</b>
<p>If you add the <code class="inline"><span class="w">stringify</span></code>
 import option to your <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span></code>
 declaration,
then using a threads object in a string or a string context (e.g., as a hash
key) will cause its ID to be used as the value:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span> <span class="q">qw(stringify)</span><span class="sc">;</span></li><li></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">&quot;Thread $thr started\n&quot;</span><span class="s">)</span><span class="sc">;</span>  <span class="c"># Prints: Thread 1 started</span></li></ol></pre></li>
<li><a name="threads-%3eobject(%24tid)"></a><b>threads-&gt;object($tid)</b>
<p>This will return the <i>threads</i> object for the <i>active</i> thread associated
with the specified thread ID.  If <code class="inline"><span class="i">$tid</span></code>
 is the value for the current thread,
then this call works the same as <code class="inline"><span class="i">-&gt;self</span><span class="s">(</span><span class="s">)</span></code>
.  Otherwise, returns <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>
if there is no thread associated with the TID, if the thread is joined or
detached, if no TID is specified or if the specified TID is undef.</p>
</li>
<li><a name="threads-%3eyield()"></a><b>threads-&gt;yield()</b>
<p>This is a suggestion to the OS to let this thread yield CPU time to other
threads.  What actually happens is highly dependent upon the underlying
thread implementation.</p>
<p>You may do <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span> <span class="q">qw(yield)</span></code>
, and then just use <code class="inline"><span class="i">yield</span><span class="s">(</span><span class="s">)</span></code>
 in your
code.</p>
</li>
<li><a name="threads-%3elist()"></a><b>threads-&gt;list()</b>
</li>
<li><a name="threads-%3elist(threads%3a%3aall)"></a><b>threads-&gt;list(threads::all)</b>
</li>
<li><a name="threads-%3elist(threads%3a%3arunning)"></a><b>threads-&gt;list(threads::running)</b>
</li>
<li><a name="threads-%3elist(threads%3a%3ajoinable)"></a><b>threads-&gt;list(threads::joinable)</b>
<p>With no arguments (or using <code class="inline"><span class="w">threads::all</span></code>
) and in a list context, returns a
list of all non-joined, non-detached <i>threads</i> objects.  In a scalar context,
returns a count of the same.</p>
<p>With a <i>true</i> argument (using <code class="inline"><span class="w">threads::running</span></code>
), returns a list of all
non-joined, non-detached <i>threads</i> objects that are still running.</p>
<p>With a <i>false</i> argument (using <code class="inline"><span class="w">threads::joinable</span></code>
), returns a list of all
non-joined, non-detached <i>threads</i> objects that have finished running (i.e.,
for which <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 will not <i>block</i>).</p>
</li>
<li><a name="%24thr1-%3eequal(%24thr2)"></a><b>$thr1-&gt;equal($thr2)</b>
<p>Tests if two threads objects are the same thread or not.  This is overloaded
to the more natural forms:</p>
<pre class="verbatim"><ol><li>    if <span class="s">(</span><span class="i">$thr1</span> == <span class="i">$thr2</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">&quot;Threads are the same\n&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li><li>    <span class="c"># or</span></li><li>    if <span class="s">(</span><span class="i">$thr1</span> != <span class="i">$thr2</span><span class="s">)</span> <span class="s">{</span></li><li>        <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">&quot;Threads differ\n&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="s">}</span></li></ol></pre><p>(Thread comparison is based on thread IDs.)</p>
</li>
<li><a name="async-BLOCK%3b"></a><b>async BLOCK;</b>
<p><code class="inline"><span class="w">async</span></code>
 creates a thread to execute the block immediately following
it.  This block is treated as an anonymous subroutine, and so must have a
semicolon after the closing brace.  Like <code class="inline"><span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">)</span></code>
, <code class="inline"><span class="w">async</span></code>

returns a <i>threads</i> object.</p>
</li>
<li><a name="%24thr-%3eerror()"></a><b>$thr-&gt;error()</b>
<p>Threads are executed in an <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> context.  This method will return <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code>
if the thread terminates <i>normally</i>.  Otherwise, it returns the value of
<code class="inline"><span class="i">$@</span></code>
 associated with the thread's execution status in its <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> context.</p>
</li>
<li><a name="%24thr-%3e_handle()"></a><b>$thr-&gt;_handle()</b>
<p>This <i>private</i> method returns a pointer (i.e., the memory location expressed
as an unsigned integer) to the internal thread structure associated with a
threads object.  For Win32, this is a pointer to the <code class="inline"><span class="w">HANDLE</span></code>
 value returned
by <code class="inline"><span class="w">CreateThread</span></code>
 (i.e., <code class="inline"><span class="w">HANDLE</span> *</code>
); for other platforms, it is a pointer
to the <code class="inline"><span class="w">pthread_t</span></code>
 structure used in the <code class="inline"><span class="w">pthread_create</span></code>
 call (i.e.,
<code class="inline"><span class="w">pthread_t</span> *</code>
).</p>
<p>This method is of no use for general Perl threads programming.  Its intent is
to provide other (XS-based) thread modules with the capability to access, and
possibly manipulate, the underlying thread structure associated with a Perl
thread.</p>
</li>
<li><a name="threads-%3e_handle()"></a><b>threads-&gt;_handle()</b>
<p>Class method that allows a thread to obtain its own <i>handle</i>.</p>
</li>
</ul>
<a name="EXITING-A-THREAD"></a><h1>EXITING A THREAD</h1>
<p>The usual method for terminating a thread is to
<a href="functions/return.html">return EXPR</a> from the entry point function with the
appropriate return value(s).</p>
<ul>
<li><a name="threads-%3eexit()"></a><b>threads-&gt;exit()</b>
<p>If needed, a thread can be exited at any time by calling
<code class="inline"><span class="w">threads</span><span class="w">-&gt;exit</span><span class="s">(</span><span class="s">)</span></code>
.  This will cause the thread to return <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> in a
scalar context, or the empty list in a list context.</p>
<p>When called from the <i>main</i> thread, this behaves the same as <code class="inline"><a class="l_k" href="functions/exit.html">exit(0)</a></code>.</p>
</li>
<li><a name="threads-%3eexit(status)"></a><b>threads-&gt;exit(status)</b>
<p>When called from a thread, this behaves like <code class="inline"><span class="w">threads</span><span class="w">-&gt;exit</span><span class="s">(</span><span class="s">)</span></code>
 (i.e., the
exit status code is ignored).</p>
<p>When called from the <i>main</i> thread, this behaves the same as <code class="inline"><a class="l_k" href="functions/exit.html">exit(status)</a></code>.</p>
</li>
<li><a name="die()"></a><b>die()</b>
<p>Calling <code class="inline"><a class="l_k" href="functions/die.html">die()</a></code> in a thread indicates an abnormal exit for the thread.  Any
<code class="inline"><span class="i">$SIG</span>{<span class="w">__DIE__</span>}</code>
 handler in the thread will be called first, and then the
thread will exit with a warning message that will contain any arguments passed
in the <code class="inline"><a class="l_k" href="functions/die.html">die()</a></code> call.</p>
</li>
<li><a name="exit(status)"></a><b>exit(status)</b>
<p>Calling <a href="functions/exit.html">exit EXPR</a> inside a thread causes the whole
application to terminate.  Because of this, the use of <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code> inside
threaded code, or in modules that might be used in threaded applications, is
strongly discouraged.</p>
<p>If <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code> really is needed, then consider using the following:</p>
<pre class="verbatim"><ol><li>    <span class="w">threads</span><span class="w">-&gt;exit</span><span class="s">(</span><span class="s">)</span> if <span class="w">threads</span><span class="w">-&gt;can</span><span class="s">(</span><span class="q">'exit'</span><span class="s">)</span><span class="sc">;</span>   <span class="c"># Thread friendly</span></li><li>    <a class="l_k" href="functions/exit.html">exit</a><span class="s">(</span><span class="w">status</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="use-threads-'exit'-%3d%3e-'threads_only'"></a><b>use threads 'exit' =&gt; 'threads_only'</b>
<p>This globally overrides the default behavior of calling <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code> inside a
thread, and effectively causes such calls to behave the same as
<code class="inline"><span class="w">threads</span><span class="w">-&gt;exit</span><span class="s">(</span><span class="s">)</span></code>
.  In other words, with this setting, calling <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code>
causes only the thread to terminate.</p>
<p>Because of its global effect, this setting should not be used inside modules
or the like.</p>
<p>The <i>main</i> thread is unaffected by this setting.</p>
</li>
<li><a name="threads-%3ecreate(%7b'exit'-%3d%3e-'thread_only'%7d%2c-...)"></a><b>threads-&gt;create({'exit' =&gt; 'thread_only'}, ...)</b>
<p>This overrides the default behavior of <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code> inside the newly created
thread only.</p>
</li>
<li><a name="%24thr-%3eset_thread_exit_only(boolean)"></a><b>$thr-&gt;set_thread_exit_only(boolean)</b>
<p>This can be used to change the <i>exit thread only</i> behavior for a thread after
it has been created.  With a <i>true</i> argument, <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code> will cause only the
thread to exit.  With a <i>false</i> argument, <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code> will terminate the
application.</p>
<p>The <i>main</i> thread is unaffected by this call.</p>
</li>
<li><a name="threads-%3eset_thread_exit_only(boolean)"></a><b>threads-&gt;set_thread_exit_only(boolean)</b>
<p>Class method for use inside a thread to change its own behavior for <code class="inline"><a class="l_k" href="functions/exit.html">exit()</a></code>.</p>
<p>The <i>main</i> thread is unaffected by this call.</p>
</li>
</ul>
<a name="THREAD-STATE"></a><h1>THREAD STATE</h1>
<p>The following boolean methods are useful in determining the <i>state</i> of a
thread.</p>
<ul>
<li><a name="%24thr-%3eis_running()"></a><b>$thr-&gt;is_running()</b>
<p>Returns true if a thread is still running (i.e., if its entry point function
has not yet finished or exited).</p>
</li>
<li><a name="%24thr-%3eis_joinable()"></a><b>$thr-&gt;is_joinable()</b>
<p>Returns true if the thread has finished running, is not detached and has not
yet been joined.  In other words, the thread is ready to be joined, and a call
to <code class="inline"><span class="i">$thr</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 will not <i>block</i>.</p>
</li>
<li><a name="%24thr-%3eis_detached()"></a><b>$thr-&gt;is_detached()</b>
<p>Returns true if the thread has been detached.</p>
</li>
<li><a name="threads-%3eis_detached()"></a><b>threads-&gt;is_detached()</b>
<p>Class method that allows a thread to determine whether or not it is detached.</p>
</li>
</ul>
<a name="THREAD-CONTEXT"></a><h1>THREAD CONTEXT</h1>
<p>As with subroutines, the type of value returned from a thread's entry point
function may be determined by the thread's <i>context</i>:  list, scalar or void.
The thread's context is determined at thread creation.  This is necessary so
that the context is available to the entry point function via
<a href="functions/wantarray.html">wantarray</a>.  The thread may then specify a value of
the appropriate type to be returned from <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
.</p>
<a name="Explicit-context"></a><h2>Explicit context</h2>
<p>Because thread creation and thread joining may occur in different contexts, it
may be desirable to state the context explicitly to the thread's entry point
function.  This may be done by calling <code class="inline"><span class="i">-&gt;create</span><span class="s">(</span><span class="s">)</span></code>
 with a hash reference
as the first argument:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">'context'</span> <span class="cm">=&gt;</span> <span class="q">'list'</span><span class="s">}</span><span class="cm">,</span> \<span class="i">&amp;foo</span><span class="s">)</span><span class="sc">;</span></li><li>    ...</li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">@results</span> = <span class="i">$thr</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>In the above, the threads object is returned to the parent thread in scalar
context, and the thread's entry point function <code class="inline"><span class="w">foo</span></code>
 will be called in list
(array) context such that the parent thread can receive a list (array) from
the <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 call.  (<code class="inline"><span class="q">'array'</span></code>
 is synonymous with <code class="inline"><span class="q">'list'</span></code>
.)</p>
<p>Similarly, if you need the threads object, but your thread will not be
returning a value (i.e., <i>void</i> context), you would do the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">'context'</span> <span class="cm">=&gt;</span> <span class="q">'void'</span><span class="s">}</span><span class="cm">,</span> \<span class="i">&amp;foo</span><span class="s">)</span><span class="sc">;</span></li><li>    ...</li><li>    <span class="i">$thr</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>The context type may also be used as the <i>key</i> in the hash reference followed
by a <i>true</i> value:</p>
<pre class="verbatim"><ol><li>    <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">'scalar'</span> <span class="cm">=&gt;</span> <span class="n">1</span><span class="s">}</span><span class="cm">,</span> \<span class="i">&amp;foo</span><span class="s">)</span><span class="sc">;</span></li><li>    ...</li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$thr</span><span class="s">)</span> = <span class="w">threads</span><span class="w">-&gt;list</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$result</span> = <span class="i">$thr</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="Implicit-context"></a><h2>Implicit context</h2>
<p>If not explicitly stated, the thread's context is implied from the context
of the <code class="inline"><span class="i">-&gt;create</span><span class="s">(</span><span class="s">)</span></code>
 call:</p>
<pre class="verbatim"><ol><li>    <span class="c"># Create thread in list context</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$thr</span><span class="s">)</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Create thread in scalar context</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Create thread in void context</span></li><li>    <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span>...<span class="s">)</span><span class="sc">;</span></li></ol></pre><a name="%24thr-%3ewantarray()"></a><h2>$thr-&gt;wantarray()</h2>
<p>This returns the thread's context in the same manner as
<a href="functions/wantarray.html">wantarray</a>.</p>
<a name="threads-%3ewantarray()"></a><h2>threads-&gt;wantarray()</h2>
<p>Class method to return the current thread's context.  This returns the same
value as running <a href="functions/wantarray.html">wantarray</a> inside the current
thread's entry point function.</p>
<a name="THREAD-STACK-SIZE"></a><h1>THREAD STACK SIZE</h1>
<p>The default per-thread stack size for different platforms varies
significantly, and is almost always far more than is needed for most
applications.  On Win32, Perl's makefile explicitly sets the default stack to
16 MB; on most other platforms, the system default is used, which again may be
much larger than is needed.</p>
<p>By tuning the stack size to more accurately reflect your application's needs,
you may significantly reduce your application's memory usage, and increase the
number of simultaneously running threads.</p>
<p>Note that on Windows, address space allocation granularity is 64 KB,
therefore, setting the stack smaller than that on Win32 Perl will not save any
more memory.</p>
<ul>
<li><a name="threads-%3eget_stack_size()%3b"></a><b>threads-&gt;get_stack_size();</b>
<p>Returns the current default per-thread stack size.  The default is zero, which
means the system default stack size is currently in use.</p>
</li>
<li><a name="%24size-%3d-%24thr-%3eget_stack_size()%3b"></a><b>$size = $thr-&gt;get_stack_size();</b>
<p>Returns the stack size for a particular thread.  A return value of zero
indicates the system default stack size was used for the thread.</p>
</li>
<li><a name="%24old_size-%3d-threads-%3eset_stack_size(%24new_size)%3b"></a><b>$old_size = threads-&gt;set_stack_size($new_size);</b>
<p>Sets a new default per-thread stack size, and returns the previous setting.</p>
<p>Some platforms have a minimum thread stack size.  Trying to set the stack size
below this value will result in a warning, and the minimum stack size will be
used.</p>
<p>Some Linux platforms have a maximum stack size.  Setting too large of a stack
size will cause thread creation to fail.</p>
<p>If needed, <code class="inline"><span class="i">$new_size</span></code>
 will be rounded up to the next multiple of the memory
page size (usually 4096 or 8192).</p>
<p>Threads created after the stack size is set will then either call
<code class="inline"><span class="i">pthread_attr_setstacksize</span><span class="s">(</span><span class="s">)</span></code>
 <i>(for pthreads platforms)</i>, or supply the
stack size to <code class="inline"><span class="i">CreateThread</span><span class="s">(</span><span class="s">)</span></code>
 <i>(for Win32 Perl)</i>.</p>
<p>(Obviously, this call does not affect any currently extant threads.)</p>
</li>
<li><a name="use-threads-('stack_size'-%3d%3e-VALUE)%3b"></a><b>use threads ('stack_size' =&gt; VALUE);</b>
<p>This sets the default per-thread stack size at the start of the application.</p>
</li>
<li><a name="%24ENV%7b'PERL5_ITHREADS_STACK_SIZE'%7d"></a><b>$ENV{'PERL5_ITHREADS_STACK_SIZE'}</b>
<p>The default per-thread stack size may be set at the start of the application
through the use of the environment variable <code class="inline"><span class="w">PERL5_ITHREADS_STACK_SIZE</span></code>
:</p>
<pre class="verbatim"><ol><li>    PERL5_ITHREADS_STACK_SIZE=1048576</li><li>    export PERL5_ITHREADS_STACK_SIZE</li><li>    perl -e'use threads; print(threads-&gt;get_stack_size(), "\n")'</li></ol></pre><p>This value overrides any <code class="inline"><span class="w">stack_size</span></code>
 parameter given to <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span></code>
.  Its
primary purpose is to permit setting the per-thread stack size for legacy
threaded applications.</p>
</li>
<li><a name="threads-%3ecreate(%7b'stack_size'-%3d%3e-VALUE%7d%2c-FUNCTION%2c-ARGS)"></a><b>threads-&gt;create({'stack_size' =&gt; VALUE}, FUNCTION, ARGS)</b>
<p>To specify a particular stack size for any individual thread, call
<code class="inline"><span class="i">-&gt;create</span><span class="s">(</span><span class="s">)</span></code>
 with a hash reference as the first argument:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">&#39;stack_size&#39;</span> <span class="cm">=&gt;</span> <span class="n">32</span>*<span class="n">4096</span><span class="s">}</span><span class="cm">,</span></li><li>                              \<span class="i">&amp;foo</span><span class="cm">,</span> <span class="i">@args</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="%24thr2-%3d-%24thr1-%3ecreate(FUNCTION%2c-ARGS)"></a><b>$thr2 = $thr1-&gt;create(FUNCTION, ARGS)</b>
<p>This creates a new thread (<code class="inline"><span class="i">$thr2</span></code>
) that inherits the stack size from an
existing thread (<code class="inline"><span class="i">$thr1</span></code>
).  This is shorthand for the following:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$stack_size</span> = <span class="i">$thr1</span><span class="i">-&gt;get_stack_size</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr2</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="s">{</span><span class="q">&#39;stack_size&#39;</span> <span class="cm">=&gt;</span> <span class="i">$stack_size</span><span class="s">}</span><span class="cm">,</span></li><li>                               <span class="w">FUNCTION</span><span class="cm">,</span> <span class="w">ARGS</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
</ul>
<a name="THREAD-SIGNALLING"></a><h1>THREAD SIGNALLING</h1>
<p>When safe signals is in effect (the default behavior - see <a href="#Unsafe-signals">Unsafe signals</a>
for more details), then signals may be sent and acted upon by individual
threads.</p>
<ul>
<li><a name="%24thr-%3ekill('SIG...')%3b"></a><b>$thr-&gt;kill('SIG...');</b>
<p>Sends the specified signal to the thread.  Signal names and (positive) signal
numbers are the same as those supported by
<a href="functions/kill.html">kill SIGNAL, LIST</a>.  For example, 'SIGTERM', 'TERM' and
(depending on the OS) 15 are all valid arguments to <code class="inline"><span class="i">-&gt;kill</span><span class="s">(</span><span class="s">)</span></code>
.</p>
<p>Returns the thread object to allow for method chaining:</p>
<pre class="verbatim"><ol><li>    <span class="i">$thr</span><span class="i">-&gt;kill</span><span class="s">(</span><span class="q">'SIG...'</span><span class="s">)</span><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
</ul>
<p>Signal handlers need to be set up in the threads for the signals they are
expected to act upon.  Here's an example for <i>cancelling</i> a thread:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span><span class="sc">;</span></li><li></li><li><a name="thr_func"></a>    sub <span class="m">thr_func</span></li><li>    <span class="s">{</span></li><li>        <span class="c"># Thread &#39;cancellation&#39; signal handler</span></li><li>        <span class="i">$SIG</span>{<span class="q">&#39;KILL&#39;</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="w">threads</span><span class="w">-&gt;exit</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span> <span class="s">}</span><span class="sc">;</span></li><li></li><li>        ...</li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># Create a thread</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="q">&#39;thr_func&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    ...</li><li></li><li>    <span class="c"># Signal the thread to terminate, and then detach</span></li><li>    <span class="c"># it so that it will get cleaned up automatically</span></li><li>    <span class="i">$thr</span><span class="i">-&gt;kill</span><span class="s">(</span><span class="q">&#39;KILL&#39;</span><span class="s">)</span><span class="i">-&gt;detach</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Here's another simplistic example that illustrates the use of thread
signalling in conjunction with a semaphore to provide rudimentary <i>suspend</i>
and <i>resume</i> capabilities:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">threads</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Thread::Semaphore</span><span class="sc">;</span></li><li></li><li><a name="thr_func"></a>    sub <span class="m">thr_func</span></li><li>    <span class="s">{</span></li><li>        <a class="l_k" href="functions/my.html">my</a> <span class="i">$sema</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li></li><li>        <span class="c"># Thread &#39;suspend/resume&#39; signal handler</span></li><li>        <span class="i">$SIG</span>{<span class="q">&#39;STOP&#39;</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>            <span class="i">$sema</span><span class="i">-&gt;down</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>      <span class="c"># Thread suspended</span></li><li>            <span class="i">$sema</span><span class="i">-&gt;up</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span>        <span class="c"># Thread resumes</span></li><li>        <span class="s">}</span><span class="sc">;</span></li><li></li><li>        ...</li><li>    <span class="s">}</span></li><li></li><li>    <span class="c"># Create a semaphore and pass it to a thread</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$sema</span> = <span class="w">Thread::Semaphore</span><span class="w">-&gt;new</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><span class="q">&#39;thr_func&#39;</span><span class="cm">,</span> <span class="i">$sema</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    <span class="c"># Suspend the thread</span></li><li>    <span class="i">$sema</span><span class="i">-&gt;down</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="i">$thr</span><span class="i">-&gt;kill</span><span class="s">(</span><span class="q">&#39;STOP&#39;</span><span class="s">)</span><span class="sc">;</span></li><li></li><li>    ...</li><li></li><li>    <span class="c"># Allow the thread to continue</span></li><li>    <span class="i">$sema</span><span class="i">-&gt;up</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>CAVEAT:  The thread signalling capability provided by this module does not
actually send signals via the OS.  It <i>emulates</i> signals at the Perl-level
such that signal handlers are called in the appropriate thread.  For example,
sending <code class="inline"><span class="i">$thr</span><span class="i">-&gt;kill</span><span class="s">(</span><span class="q">'STOP'</span><span class="s">)</span></code>
 does not actually suspend a thread (or the
whole process), but does cause a <code class="inline"><span class="i">$SIG</span>{<span class="q">'STOP'</span>}</code>
 handler to be called in that
thread (as illustrated above).</p>
<p>As such, signals that would normally not be appropriate to use in the
<code class="inline"><a class="l_k" href="functions/kill.html">kill()</a></code> command (e.g., <code class="inline"><a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'KILL'</span><span class="cm">,</span> <span class="i">$$</span><span class="s">)</span></code>
) are okay to use with the
<code class="inline"><span class="i">-&gt;kill</span><span class="s">(</span><span class="s">)</span></code>
 method (again, as illustrated above).</p>
<p>Correspondingly, sending a signal to a thread does not disrupt the operation
the thread is currently working on:  The signal will be acted upon after the
current operation has completed.  For instance, if the thread is <i>stuck</i> on
an I/O call, sending it a signal will not cause the I/O call to be interrupted
such that the signal is acted up immediately.</p>
<p>Sending a signal to a terminated/finished thread is ignored.</p>
<a name="WARNINGS"></a><h1>WARNINGS</h1>
<ul>
<li><a name="Perl-exited-with-active-threads%3a"></a><b>Perl exited with active threads:</b>
<p>If the program exits without all threads having either been joined or
detached, then this warning will be issued.</p>
<p>NOTE:  If the <i>main</i> thread exits, then this warning cannot be suppressed
using <code class="inline"><a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">'threads'</span><span class="sc">;</span></code>
 as suggested below.</p>
</li>
<li><a name="Thread-creation-failed%3a-pthread_create-returned-%23"></a><b>Thread creation failed: pthread_create returned #</b>
<p>See the appropriate <i>man</i> page for <code class="inline"><span class="w">pthread_create</span></code>
 to determine the actual
cause for the failure.</p>
</li>
<li><a name="Thread-%23-terminated-abnormally%3a-..."></a><b>Thread # terminated abnormally: ...</b>
<p>A thread terminated in some manner other than just returning from its entry
point function, or by using <code class="inline"><span class="w">threads</span><span class="w">-&gt;exit</span><span class="s">(</span><span class="s">)</span></code>
.  For example, the thread
may have terminated because of an error, or by using <code class="inline"><a class="l_k" href="functions/die.html">die</a></code>.</p>
</li>
<li><a name="Using-minimum-thread-stack-size-of-%23"></a><b>Using minimum thread stack size of #</b>
<p>Some platforms have a minimum thread stack size.  Trying to set the stack size
below this value will result in the above warning, and the stack size will be
set to the minimum.</p>
</li>
<li><a name="Thread-creation-failed%3a-pthread_attr_setstacksize(_SIZE_)-returned-22"></a><b>Thread creation failed: pthread_attr_setstacksize(<i>SIZE</i>) returned 22</b>
<p>The specified <i>SIZE</i> exceeds the system's maximum stack size.  Use a smaller
value for the stack size.</p>
</li>
</ul>
<p>If needed, thread warnings can be suppressed by using:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/no.html">no</a> <span class="w">warnings</span> <span class="q">'threads'</span><span class="sc">;</span></li></ol></pre><p>in the appropriate scope.</p>
<a name="ERRORS"></a><h1>ERRORS</h1>
<ul>
<li><a name="This-Perl-not-built-to-support-threads"></a><b>This Perl not built to support threads</b>
<p>The particular copy of Perl that you're trying to use was not built using the
<code class="inline"><span class="w">useithreads</span></code>
 configuration option.</p>
<p>Having threads support requires all of Perl and all of the XS modules in the
Perl installation to be rebuilt; it is not just a question of adding the
<a href="threads.html">threads</a> module (i.e., threaded and non-threaded Perls are binary
incompatible).</p>
</li>
<li><a name="Cannot-change-stack-size-of-an-existing-thread"></a><b>Cannot change stack size of an existing thread</b>
<p>The stack size of currently extant threads cannot be changed, therefore, the
following results in the above error:</p>
<pre class="verbatim"><ol><li>    <span class="i">$thr</span><span class="i">-&gt;set_stack_size</span><span class="s">(</span><span class="i">$size</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="Cannot-signal-threads-without-safe-signals"></a><b>Cannot signal threads without safe signals</b>
<p>Safe signals must be in effect to use the <code class="inline"><span class="i">-&gt;kill</span><span class="s">(</span><span class="s">)</span></code>
 signalling method.
See <a href="#Unsafe-signals">Unsafe signals</a> for more details.</p>
</li>
<li><a name="Unrecognized-signal-name%3a-..."></a><b>Unrecognized signal name: ...</b>
<p>The particular copy of Perl that you're trying to use does not support the
specified signal being used in a <code class="inline"><span class="i">-&gt;kill</span><span class="s">(</span><span class="s">)</span></code>
 call.</p>
</li>
</ul>
<a name="BUGS-AND-LIMITATIONS"></a><h1>BUGS AND LIMITATIONS</h1>
<p>Before you consider posting a bug report, please consult, and possibly post a
message to the discussion forum to see if what you've encountered is a known
problem.</p>
<ul>
<li><a name="Thread-safe-modules"></a><b>Thread-safe modules</b>
<p>See <a href="perlmod.html#Making-your-module-threadsafe">Making your module threadsafe in perlmod</a> when creating modules that may
be used in threaded applications, especially if those modules use non-Perl
data, or XS code.</p>
</li>
<li><a name="Using-non-thread-safe-modules"></a><b>Using non-thread-safe modules</b>
<p>Unfortunately, you may encounter Perl modules that are not <i>thread-safe</i>.
For example, they may crash the Perl interpreter during execution, or may dump
core on termination.  Depending on the module and the requirements of your
application, it may be possible to work around such difficulties.</p>
<p>If the module will only be used inside a thread, you can try loading the
module from inside the thread entry point function using <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> (and
<code class="inline"><a class="l_k" href="functions/import.html">import</a></code> if needed):</p>
<pre class="verbatim"><ol><li>    sub thr_func</li><li>    {</li><li>        require Unsafe::Module</li><li>        # Unsafe::Module-&gt;import(...);</li><li></li><li>        ....</li><li>    }</li></ol></pre><p>If the module is needed inside the <i>main</i> thread, try modifying your
application so that the module is loaded (again using <code class="inline"><a class="l_k" href="functions/require.html">require</a></code> and
<code class="inline"><span class="i">-&gt;import</span><span class="s">(</span><span class="s">)</span></code>
) after any threads are started, and in such a way that no
other threads are started afterwards.</p>
<p>If the above does not work, or is not adequate for your application, then file
a bug report on <a href="http://rt.cpan.org/Public/">http://rt.cpan.org/Public/</a> against the problematic module.</p>
</li>
<li><a name="Memory-consumption"></a><b>Memory consumption</b>
<p>On most systems, frequent and continual creation and destruction of threads
can lead to ever-increasing growth in the memory footprint of the Perl
interpreter.  While it is simple to just launch threads and then
<code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 or <code class="inline"><span class="i">-&gt;detach</span><span class="s">(</span><span class="s">)</span></code>
 them, for long-lived applications, it is
better to maintain a pool of threads, and to reuse them for the work needed,
using <a href="Thread/Queue.html">queues</a> to notify threads of pending work.  The CPAN
distribution of this module contains a simple example
(<i>examples/pool_reuse.pl</i>) illustrating the creation, use and monitoring of a
pool of <i>reusable</i> threads.</p>
</li>
<li><a name="Current-working-directory"></a><b>Current working directory</b>
<p>On all platforms except MSWin32, the setting for the current working directory
is shared among all threads such that changing it in one thread (e.g., using
<code class="inline"><a class="l_k" href="functions/chdir.html">chdir()</a></code>) will affect all the threads in the application.</p>
<p>On MSWin32, each thread maintains its own the current working directory
setting.</p>
</li>
<li><a name="Environment-variables"></a><b>Environment variables</b>
<p>Currently, on all platforms except MSWin32, all <i>system</i> calls (e.g., using
<code class="inline"><a class="l_k" href="functions/system.html">system()</a></code> or back-ticks) made from threads use the environment variable
settings from the <i>main</i> thread.  In other words, changes made to <code class="inline"><span class="i">%ENV</span></code>
 in
a thread will not be visible in <i>system</i> calls made by that thread.</p>
<p>To work around this, set environment variables as part of the <i>system</i> call.
For example:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/my.html">my</a> <span class="i">$msg</span> = <span class="q">'hello'</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/system.html">system</a><span class="s">(</span><span class="q">&quot;FOO=$msg; echo \$FOO&quot;</span><span class="s">)</span><span class="sc">;</span>   <span class="c"># Outputs 'hello' to STDOUT</span></li></ol></pre><p>On MSWin32, each thread maintains its own set of environment variables.</p>
</li>
<li><a name="Catching-signals"></a><b>Catching signals</b>
<p>Signals are <i>caught</i> by the main thread (thread ID = 0) of a script.
Therefore, setting up signal handlers in threads for purposes other than
<a href="#THREAD-SIGNALLING">THREAD SIGNALLING</a> as documented above will not accomplish what is
intended.</p>
<p>This is especially true if trying to catch <code class="inline"><span class="w">SIGALRM</span></code>
 in a thread.  To handle
alarms in threads, set up a signal handler in the main thread, and then use
<a href="#THREAD-SIGNALLING">THREAD SIGNALLING</a> to relay the signal to the thread:</p>
<pre class="verbatim"><ol><li>  <span class="c"># Create thread with a task that may time out</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$thr</span> = <span class="w">threads</span><span class="w">-&gt;create</span><span class="s">(</span><a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span></li><li>      <span class="w">threads</span><span class="w">-&gt;yield</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/eval.html">eval</a> <span class="s">{</span></li><li>          <span class="i">$SIG</span>{<span class="w">ALRM</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/die.html">die</a><span class="s">(</span><span class="q">&quot;Timeout\n&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="s">}</span><span class="sc">;</span></li><li>          <a class="l_k" href="functions/alarm.html">alarm</a><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="sc">;</span></li><li>          ...  <span class="c"># Do work here</span></li><li>          <a class="l_k" href="functions/alarm.html">alarm</a><span class="s">(</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li>      <span class="s">}</span><span class="sc">;</span></li><li>      <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$@</span> =~ <span class="q">/Timeout/</span><span class="s">)</span> <span class="s">{</span></li><li>          <a class="l_k" href="functions/warn.html">warn</a><span class="s">(</span><span class="q">&quot;Task in thread timed out\n&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>      <span class="s">}</span></li><li>  <span class="s">}</span><span class="sc">;</span></li><li></li><li>  <span class="c"># Set signal handler to relay SIGALRM to thread</span></li><li>  <span class="i">$SIG</span>{<span class="w">ALRM</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$thr</span><span class="i">-&gt;kill</span><span class="s">(</span><span class="q">&#39;ALRM&#39;</span><span class="s">)</span> <span class="s">}</span><span class="sc">;</span></li><li></li><li>  ... <span class="c"># Main thread continues working</span></li></ol></pre></li>
<li><a name="Parent-child-threads"></a><b>Parent-child threads</b>
<p>On some platforms, it might not be possible to destroy <i>parent</i> threads while
there are still existing <i>child</i> threads.</p>
</li>
<li><a name="Creating-threads-inside-special-blocks"></a><b>Creating threads inside special blocks</b>
<p>Creating threads inside <code class="inline">BEGIN</code>
, <code class="inline">CHECK</code>
 or <code class="inline">INIT</code>
 blocks should not be
relied upon.  Depending on the Perl version and the application code, results
may range from success, to (apparently harmless) warnings of leaked scalar, or
all the way up to crashing of the Perl interpreter.</p>
</li>
<li><a name="Unsafe-signals"></a><b>Unsafe signals</b>
<p>Since Perl 5.8.0, signals have been made safer in Perl by postponing their
handling until the interpreter is in a <i>safe</i> state.  See
<a href="perl58delta.html#Safe-Signals">Safe Signals in perl58delta</a> and <a href="perlipc.html#Deferred-Signals-(Safe-Signals)">Deferred Signals (Safe Signals) in perlipc</a>
for more details.</p>
<p>Safe signals is the default behavior, and the old, immediate, unsafe
signalling behavior is only in effect in the following situations:</p>
<ul>
<li><a name="*-Perl-has-been-built-with-PERL_OLD_SIGNALS-(see-perl--V)."></a><b>Perl has been built with <code class="inline"><span class="w">PERL_OLD_SIGNALS</span></code>
 (see <code class="inline"><span class="w">perl</span> -<span class="w">V</span></code>
).</b>
</li>
<li><a name="*-The-environment-variable-PERL_SIGNALS-is-set-to-unsafe-(see-the-perlrun%2f%22PERL_SIGNALS%22-manpage)."></a><b>The environment variable <code class="inline"><span class="w">PERL_SIGNALS</span></code>
 is set to <code class="inline"><span class="w">unsafe</span></code>

(see <a href="perlrun.html#PERL_SIGNALS">PERL_SIGNALS in perlrun</a>).</b>
</li>
<li><a name="*-The-module-the-Perl%3a%3aUnsafe%3a%3aSignals-manpage-is-used."></a><b>The module <a href="http://search.cpan.org/perldoc/Perl::Unsafe::Signals">Perl::Unsafe::Signals</a> is used.</b>
</li>
</ul>
<p>If unsafe signals is in effect, then signal handling is not thread-safe, and
the <code class="inline"><span class="i">-&gt;kill</span><span class="s">(</span><span class="s">)</span></code>
 signalling method cannot be used.</p>
</li>
<li><a name="Returning-closures-from-threads"></a><b>Returning closures from threads</b>
<p>Returning closures from threads should not be relied upon.  Depending on the
Perl version and the application code, results may range from success, to
(apparently harmless) warnings of leaked scalar, or all the way up to crashing
of the Perl interpreter.</p>
</li>
<li><a name="Returning-objects-from-threads"></a><b>Returning objects from threads</b>
<p>Returning objects from threads does not work.  Depending on the classes
involved, you may be able to work around this by returning a serialized
version of the object (e.g., using <a href="Data/Dumper.html">Data::Dumper</a> or <a href="Storable.html">Storable</a>), and then
reconstituting it in the joining thread.  If you're using Perl 5.10.0 or
later, and if the class supports <a href="threads/shared.html#OBJECTS">shared objects</a>,
you can pass them via <a href="Thread/Queue.html">shared queues</a>.</p>
</li>
<li><a name="END-blocks-in-threads"></a><b>END blocks in threads</b>
<p>It is possible to add <a href="perlmod.html#BEGIN%2c-UNITCHECK%2c-CHECK%2c-INIT-and-END">END blocks</a> to threads by using <a href="functions/require.html">require VERSION</a> or
<a href="functions/eval.html">eval EXPR</a> with the appropriate code.  These <code class="inline">END</code>
 blocks
will then be executed when the thread's interpreter is destroyed (i.e., either
during a <code class="inline"><span class="i">-&gt;join</span><span class="s">(</span><span class="s">)</span></code>
 call, or at program termination).</p>
<p>However, calling any <a href="threads.html">threads</a> methods in such an <code class="inline">END</code>
 block will most
likely <i>fail</i> (e.g., the application may hang, or generate an error) due to
mutexes that are needed to control functionality within the <a href="threads.html">threads</a> module.</p>
<p>For this reason, the use of <code class="inline">END</code>
 blocks in threads is <b>strongly</b>
discouraged.</p>
</li>
<li><a name="Open-directory-handles"></a><b>Open directory handles</b>
<p>In perl 5.14 and higher, on systems other than Windows that do
not support the <code class="inline"><span class="w">fchdir</span></code>
 C function, directory handles (see
<a href="functions/opendir.html">opendir DIRHANDLE,EXPR</a>) will not be copied to new
threads. You can use the <code class="inline"><span class="w">d_fchdir</span></code>
 variable in <a href="Config.html">Config.pm</a> to
determine whether your system supports it.</p>
<p>In prior perl versions, spawning threads with open directory handles would
crash the interpreter.
<a href="http://search.cpan.org/perldoc/http:#%2frt.perl.org%2frt3%2fPublic%2fBug%2fDisplay.html%3fid%3d75154">[perl #75154]</a></p>
</li>
<li><a name="Detached-threads-and-global-destruction"></a><b>Detached threads and global destruction</b>
<p>If the main thread exits while there are detached threads which are still
running, then Perl's global destruction phase is not executed because
otherwise certain global structures that control the operation of threads and
that are allocated in the main thread's memory may get destroyed before the
detached thread is destroyed.</p>
<p>If you are using any code that requires the execution of the global
destruction phase for clean up (e.g., removing temp files), then do not use
detached threads, but rather join all threads before exiting the program.</p>
</li>
<li><a name="Perl-Bugs-and-the-CPAN-Version-of-the-threads-manpage"></a><b>Perl Bugs and the CPAN Version of <a href="threads.html">threads</a></b>
<p>Support for threads extends beyond the code in this module (i.e.,
<i>threads.pm</i> and <i>threads.xs</i>), and into the Perl interpreter itself.  Older
versions of Perl contain bugs that may manifest themselves despite using the
latest version of <a href="threads.html">threads</a> from CPAN.  There is no workaround for this other
than upgrading to the latest version of Perl.</p>
<p>Even with the latest version of Perl, it is known that certain constructs
with threads may result in warning messages concerning leaked scalars or
unreferenced scalars.  However, such warnings are harmless, and may safely be
ignored.</p>
<p>You can search for <a href="threads.html">threads</a> related bug reports at
<a href="http://rt.cpan.org/Public/">http://rt.cpan.org/Public/</a>.  If needed submit any new bugs, problems,
patches, etc. to: <a href="http://rt.cpan.org/Public/Dist/Display.html?Name=threads">http://rt.cpan.org/Public/Dist/Display.html?Name=threads</a></p>
</li>
</ul>
<a name="REQUIREMENTS"></a><h1>REQUIREMENTS</h1>
<p>Perl 5.8.0 or later</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p>threads on MetaCPAN:
<a href="https://metacpan.org/release/threads">https://metacpan.org/release/threads</a></p>
<p>Code repository for CPAN distribution:
<a href="https://github.com/Dual-Life/threads">https://github.com/Dual-Life/threads</a></p>
<p><a href="threads/shared.html">threads::shared</a>, <a href="perlthrtut.html">perlthrtut</a></p>
<p><a href="http://www.perl.com/pub/a/2002/06/11/threads.html">http://www.perl.com/pub/a/2002/06/11/threads.html</a> and
<a href="http://www.perl.com/pub/a/2002/09/04/threads.html">http://www.perl.com/pub/a/2002/09/04/threads.html</a></p>
<p>Perl threads mailing list:
<a href="http://lists.perl.org/list/ithreads.html">http://lists.perl.org/list/ithreads.html</a></p>
<p>Stack size discussion:
<a href="http://www.perlmonks.org/?node_id=532956">http://www.perlmonks.org/?node_id=532956</a></p>
<p>Sample code in the <i>examples</i> directory of this distribution on CPAN.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Artur Bergman &lt;sky AT crucially DOT net&gt;</p>
<p>CPAN version produced by Jerry D. Hedden &lt;jdhedden AT cpan DOT org&gt;</p>
<a name="LICENSE"></a><h1>LICENSE</h1>
<p>threads is released under the same license as Perl.</p>
<a name="ACKNOWLEDGEMENTS"></a><h1>ACKNOWLEDGEMENTS</h1>
<p>Richard Soderberg &lt;perl AT crystalflame DOT net&gt; -
Helping me out tons, trying to find reasons for races and other weird bugs!</p>
<p>Simon Cozens &lt;simon AT brecon DOT co DOT uk&gt; -
Being there to answer zillions of annoying questions</p>
<p>Rocco Caputo &lt;troc AT netrus DOT net&gt;</p>
<p>Vipul Ved Prakash &lt;mail AT vipul DOT net&gt; -
Helping with debugging</p>
<p>Dean Arnold &lt;darnold AT presicient DOT com&gt; -
Stack size API</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#VERSION">VERSION</a><li><a href="#WARNING">WARNING</a><li><a href="#SYNOPSIS">SYNOPSIS</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#EXITING-A-THREAD">EXITING A THREAD</a><li><a href="#THREAD-STATE">THREAD STATE</a><li><a href="#THREAD-CONTEXT">THREAD CONTEXT</a><ul><li><a href="#Explicit-context">Explicit context</a><li><a href="#Implicit-context">Implicit context</a><li><a href="#%24thr-%3ewantarray()">$thr->wantarray()</a><li><a href="#threads-%3ewantarray()">threads->wantarray()</a></ul><li><a href="#THREAD-STACK-SIZE">THREAD STACK SIZE</a><li><a href="#THREAD-SIGNALLING">THREAD SIGNALLING</a><li><a href="#WARNINGS">WARNINGS</a><li><a href="#ERRORS">ERRORS</a><li><a href="#BUGS-AND-LIMITATIONS">BUGS AND LIMITATIONS</a><li><a href="#REQUIREMENTS">REQUIREMENTS</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#LICENSE">LICENSE</a><li><a href="#ACKNOWLEDGEMENTS">ACKNOWLEDGEMENTS</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'threads';
  perldoc.pageAddress = 'threads.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>