]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blob - t/10-join.t
ba86a97b31dd43d43012402f295de83ef4945163
[perl/modules/Thread-Cleanup.git] / t / 10-join.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7 use Thread::Cleanup::TestThreads;
8
9 use Test::More 'no_plan';
10
11 use Thread::Cleanup;
12
13 my %called : shared;
14 my %nums   : shared;
15
16 our $x = -1;
17
18 Thread::Cleanup::register {
19  my $tid = threads->tid;
20
21  {
22   lock %called;
23   $called{$tid}++;
24  }
25
26  my $num = do {
27   lock %nums;
28   $nums{$tid};
29  };
30
31  is $x, $num, "\$x in destructor of thread $tid";
32  local $x = $tid;
33 };
34
35 my %ran : shared;
36
37 sub cb {
38  my ($y) = @_;
39
40  my $tid = threads->tid;
41  {
42   lock %ran;
43   $ran{$tid}++;
44  }
45
46  {
47   lock %nums;
48   $nums{$tid} = $y;
49  }
50  is $x, $y, "\$x in thread $tid";
51  local $x = -$tid;
52 }
53
54
55 my @threads = map {
56  local $x = $_;
57  spawn(\&cb, $_);
58 } 0 .. 4;
59
60 my @tids = map $_->tid, @threads;
61
62 $_->join for @threads;
63
64 is $x, -1, '$x in the main thread';
65
66 for (@tids) {
67  is $ran{$_},    1, "thread $_ was run once";
68  is $called{$_}, 1, "thread $_ destructor was called once";
69 }