1use strict; 2use warnings; 3 4BEGIN { 5 use Config; 6 if (! $Config{'useithreads'}) { 7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n"); 8 exit(0); 9 } 10} 11 12use ExtUtils::testlib; 13 14sub ok { 15 my ($id, $ok, $name) = @_; 16 if (! defined($name)) { 17 $name = ''; 18 } 19 20 # You have to do it this way or VMS will get confused. 21 if ($ok) { 22 print("ok $id - $name\n"); 23 } else { 24 print("not ok $id - $name\n"); 25 printf("# Failed test at line %d\n", (caller)[2]); 26 } 27 28 return ($ok); 29} 30 31BEGIN { 32 $| = 1; 33 print("1..101\n"); ### Number of tests that will be run ### 34}; 35 36use threads; 37use threads::shared; 38ok(1, 1, 'Loaded'); 39 40### Start of Testing ### 41 42my $test_count; 43share($test_count); 44$test_count = 2; 45 46for(1..10) { 47 my $foo : shared = "foo"; 48 ok($test_count++, $foo eq "foo"); 49 threads->create(sub { $foo = "bar" })->join(); 50 ok($test_count++, $foo eq "bar"); 51 my @foo : shared = ("foo","bar"); 52 ok($test_count++, $foo[1] eq "bar"); 53 threads->create(sub { ok($test_count++, shift(@foo) eq "foo")})->join(); 54 ok($test_count++, $foo[0] eq "bar"); 55 my %foo : shared = ( foo => "bar" ); 56 ok($test_count++, $foo{foo} eq "bar"); 57 threads->create(sub { $foo{bar} = "foo" })->join(); 58 ok($test_count++, $foo{bar} eq "foo"); 59 60 threads->create(sub { $foo{array} = \@foo})->join(); 61 threads->create(sub { push @{$foo{array}}, "baz"})->join(); 62 ok($test_count++, $foo[-1] eq "baz"); 63} 64 65my $shared :shared = &share({}); 66$$shared{'foo'} = 'bar'; 67 68for(1..10) { 69 my $str1 = "$shared"; 70 my $str2 = "$shared"; 71 ok($test_count++, $str1 eq $str2, 'stringify'); 72 $str1 = $$shared{'foo'}; 73 $str2 = $$shared{'foo'}; 74 ok($test_count++, $str1 eq $str2, 'contents'); 75} 76 77exit(0); 78 79# EOF 80