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

Re^3: PerlIO file handle dup

by BrowserUk (Pope)
on Mar 07, 2017 at 23:55 UTC ( #1183899=note: print w/replies, xml ) Need Help??


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

You lose sequence.

Sorry, I hadn't realised that was a requirement. I'll think about that.

Can you do the same with hashes?

Yes. But be aware that if you don't lock the hash and insertions and deletions are going on, things can get a little strange. (Eg. Iterating the hash keys might return a key, but by the time you try to access the associate value, the key might have been deleted.

If the hash is essentially static -- only the values changing -- then only sharing the value scalars and applying locking to them individually can be much more efficient than locking the entire hash for every update.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: PerlIO file handle dup
by chris212 (Scribe) on Mar 08, 2017 at 15:52 UTC

    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://1183899]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2019-10-22 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?