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 17 # You have to do it this way or VMS will get confused. 18 if ($ok) { 19 print("ok $id - $name\n"); 20 } else { 21 print("not ok $id - $name\n"); 22 printf("# Failed test at line %d\n", (caller)[2]); 23 } 24 25 return ($ok); 26} 27 28BEGIN { 29 $| = 1; 30 print("1..16\n"); ### Number of tests that will be run ### 31}; 32 33use threads; 34use threads::shared; 35ok(1, 1, 'Loaded'); 36 37### Start of Testing ### 38 39my %hash; 40share(%hash); 41$hash{"foo"} = "bar"; 42ok(2,$hash{"foo"} eq "bar","Check hash get"); 43threads->create(sub { $hash{"bar"} = "thread1"})->join(); 44threads->create(sub { ok(3,$hash{"bar"} eq "thread1", "Check thread get and write")})->join(); 45{ 46 my $foo = delete($hash{"bar"}); 47 ok(4, $foo eq "thread1", "Check delete, want 'thread1' got '$foo'"); 48 $foo = delete($hash{"bar"}); 49 ok(5, !defined $foo, "Check delete on empty value"); 50} 51ok(6, keys %hash == 1, "Check keys"); 52$hash{"1"} = 1; 53$hash{"2"} = 2; 54$hash{"3"} = 3; 55ok(7, keys %hash == 4, "Check keys"); 56ok(8, exists($hash{"1"}), "Exist on existing key"); 57ok(9, !exists($hash{"4"}), "Exists on non existing key"); 58my %seen; 59foreach my $key ( keys %hash) { 60 $seen{$key}++; 61} 62ok(10, $seen{1} == 1, "Keys.."); 63ok(11, $seen{2} == 1, "Keys.."); 64ok(12, $seen{3} == 1, "Keys.."); 65ok(13, $seen{"foo"} == 1, "Keys.."); 66 67# bugid #24407: the stringification of the numeric 1 got allocated to the 68# wrong thread memory pool, which crashes on Windows. 69ok(14, exists $hash{1}, "Check numeric key"); 70 71threads->create(sub { %hash = () })->join(); 72ok(15, keys %hash == 0, "Check clear"); 73 74ok(16, is_shared(%hash), "Check for sharing"); 75 76exit(0); 77 78# EOF 79