]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blob - t/10-join.t
Initial import
[perl/modules/Thread-Cleanup.git] / t / 10-join.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Config qw/%Config/;
7
8 BEGIN {
9  if (!$Config{useithreads}) {
10   require Test::More;
11   Test::More->import;
12   plan(skip_all => 'This perl wasn\'t built to support threads');
13  }
14 }
15
16 use threads;
17 use threads::shared;
18
19 use Test::More tests => 5 * (2 + 2) + 1;
20
21 use Thread::Cleanup;
22
23 my %called : shared;
24 my %nums   : shared;
25
26 our $x = -1;
27
28 Thread::Cleanup::register {
29  my $tid = threads->tid;
30
31  {
32   lock %called;
33   $called{$tid}++;
34  }
35
36  my $num = do {
37   lock %nums;
38   $nums{$tid};
39  };
40
41  is $x, $num, "\$x in destructor of thread $tid";
42  local $x = $tid;
43 };
44
45 my %ran : shared;
46
47 sub cb {
48  my ($y) = @_;
49
50  my $tid = threads->tid;
51  {
52   lock %ran;
53   $ran{$tid}++;
54  }
55
56  {
57   lock %nums;
58   $nums{$tid} = $y;
59  }
60  is $x, $y, "\$x in thread $tid";
61  local $x = -$tid;
62 }
63
64 my @tids;
65
66 my @t = map {
67  local $x = $_;
68  my $thr = threads->create(\&cb, $_);
69  push @tids, $thr->tid;
70  $thr;
71 } 0 .. 4;
72
73 diag "Using threads $threads::VERSION";
74 diag "Using threads::shared $threads::shared::VERSION";
75
76 $_->join for @t;
77
78 is $x, -1, '$x in the main thread';
79
80 for (@tids) {
81  is $ran{$_},    1, "thread $_ was run once";
82  is $called{$_}, 1, "thread $_ destructor was called once";
83 }