]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blob - samples/try.pl
Support pseudo-forks more reliably
[perl/modules/Thread-Cleanup.git] / samples / try.pl
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use blib;
7
8 use Thread::Cleanup;
9
10 use threads;
11
12 $|++;
13 local $\ = "\n";
14
15 Thread::Cleanup::register {
16  my $tid = threads->tid;
17  print "finished thread $tid";
18 };
19
20 sub worker {
21  my $tid = threads->tid;
22  print "running thread $tid";
23  sleep 1;
24 }
25
26 print "begin";
27
28 my @tids;
29
30 my @t = map {
31  my $thr = threads->create(\&worker);
32  my $tid = $thr->tid;
33  push @tids, $tid;
34  print "spawned thread $tid";
35  $thr;
36 } 1 .. 3;
37
38 $t[0]->join;
39 print "joined thread $tids[0]";
40
41 $t[1]->detach;
42 print "detached thread $tids[1]";
43
44 sleep 2;
45
46 print "end";
47
48 END {
49  print "END\n";
50 }