Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: PerlIO file handle dup

by chris212 (Scribe)
on Mar 07, 2017 at 22:57 UTC ( #1183893=note: print w/replies, xml ) Need Help??


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

You lose sequence. I had to autoflush output to keep it from getting jumbled. I didn't realize you can share elements of an array without sharing the whole array. Can you do the same with hashes?

Replies are listed 'Best First'.
Re^3: PerlIO file handle dup
by BrowserUk (Pope) on Mar 07, 2017 at 23:55 UTC
    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.

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2019-10-24 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?