Skip Menu |

This queue is for tickets about the threads-shared CPAN distribution.

Report information
The Basics
Id: 42538
Status: resolved
Priority: 0/
Queue: threads-shared

People
Owner: Nobody in particular
Requestors: mgcombes [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: Unimportant
Broken in: 1.27
Fixed in: 1.28



Subject: locking a value from a hash
I am trying to have a shared hash, with multiple threads accessing it, I seems that I a can lock the hash itself but none of its contents my %hash :shared; my $a = 'key'; $hash{$a} = 1; lock %hash; # work ! print '$hash{$a} is ', is_shared($hash{$a})?'shared':'NOT SHARED',"\n"; lock $hash{$a}; # runtime error ! 1; is there a work arround this ? Thanks +Michel (see attached code for a more complete exemple)
Subject: mt.pl
#!perl use threads; use threads::shared; my %seen :shared = (); my %hash :shared; my $a = 'key'; $hash{$a} = 1; lock %hash; # work ! print '$hash{$a} is ', is_shared($hash{$a})?'shared':'NOT SHARED',"\n"; lock $hash{$a}; # runtime error ! exit; for $t (0 .. 9) { my $th = threads->create(\&count); } sleep 1; for my $th (threads->list(0)) { printf 'th:%2d : found %d duplicates'."\n", $th->tid,$th->join; } use YAML; printf "seen: %s.\n",YAML::Dump(\%seen); sub count { my $total = 0; for (0 .. 99) { my $a = int rand(200); if (exists $seen{$a}) { lock $seen{$a}; # <--- ERROR : lock can only be used on shared values $seen{$a}++; threads->yield() if (rand (1) > 0.5); $total++; } else { lock %seen; threads->yield() if (rand (1) > 0.5); $seen{$a} = 1; } } return $total; }
On Mon Jan 19 16:16:57 2009, mgc wrote: Show quoted text
> I am trying to have a shared hash, with multiple threads accessing it, > I seems that I a can lock the hash itself but none of its contents > > my %hash :shared; > > my $a = 'key'; > $hash{$a} = 1; > lock %hash; # work ! > > print '$hash{$a} is ', is_shared($hash{$a})?'shared':'NOT SHARED',"\n"; > lock $hash{$a}; # runtime error !
Yes, that's how it works. You can lock a container variable, but you cannot lock individual elements. This is a documentation bug.
Show quoted text
> > I seems that I a can lock the hash itself but none of its contents
Show quoted text
> Yes, that's how it works. You can lock a container variable, but you > cannot lock individual elements. This is a documentation bug.
This doc fix is in 1.28.