Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^4: PerlIO file handle dup

by chris212 (Scribe)
on Mar 08, 2017 at 15:52 UTC ( #1183950=note: print w/replies, xml ) Need Help??


in reply to Re^3: PerlIO file handle dup
in thread PerlIO file handle dup

Actually it looks like if the hash itself is not shared, any changes to keys will not affect other running threads. This includes deleting keys (the key/value will still be there in other threads). Only changes to the shared values will affect other running threads (including undefining them).

use strict; use threads; use threads::shared; my %hash = ( 'a' => 'foo', 'b' => 'foo', 'c' => 'foo', 'd' => 'foo' ); share($hash{$_}) foreach('a'..'f'); my $tid = 0; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } my $th1 = async { $tid = 1; print "thread $tid started\n"; sleep 2; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } print "delete b from thread $tid\n"; delete $hash{'b'}; print "undefine d from thread $tid\n"; undef $hash{'d'}; print "setting f => foo in thread $tid\n"; $hash{'f'} = 'foo'; }; my $th2 = async { $tid = 2; print "thread $tid started\n"; sleep 3; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } }; sleep 1; print "delete a from thread $tid\n"; delete $hash{'a'}; print "undefine c from thread $tid\n"; undef $hash{'c'}; print "setting e => foo in thread $tid\n"; $hash{'e'} = 'foo'; $th1->join(); $th2->join(); foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } }
a => foo in thread 0 b => foo in thread 0 c => foo in thread 0 d => foo in thread 0 e => undef in thread 0 f => undef in thread 0 thread 1 started thread 2 started delete a from thread 0 undefine c from thread 0 setting e => foo in thread 0 a => foo in thread 1 b => foo in thread 1 c => undef in thread 1 d => foo in thread 1 e => foo in thread 1 f => undef in thread 1 delete b from thread 1 undefine d from thread 1 setting f => foo in thread 1 a => foo in thread 2 b => foo in thread 2 c => undef in thread 2 d => undef in thread 2 e => foo in thread 2 f => foo in thread 2 a does not exist in thread 0 b => foo in thread 0 c => undef in thread 0 d => undef in thread 0 e => foo in thread 0 f => foo in thread 0

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1183950]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2019-10-18 09:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?