Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Using clone/dclone with threads

by castaway (Parson)
on Nov 22, 2004 at 06:09 UTC ( [id://409499]=note: print w/replies, xml ) Need Help??


in reply to Using clone/dclone with threads

Can you write a runnable example with actual threads? Then it would be easier to see if its your mistake, and if not, maybe we can play with fixing it..

Have you tried checking that $cfg::sessions{A} is indeed a reference? try printing ref($cfg::sessions{A}) there and see what it says.

C.

Replies are listed 'Best First'.
Re^2: Using clone/dclone with threads
by Anonymous Monk on Nov 23, 2004 at 23:11 UTC
    FYI, here's some code. Uncomment/comment Clone/Storable, clone/dclone as reqd:

    #!/usr/bin/perl -w + use strict; use threads; use threads::shared; use Clone qw(clone); #use Storable qw(dclone); + my ( $thr1, # thread handle $key, $v1, $v2, $v3, $tmp1, $tmp2, $key1, $key2, $key3 ); + # pkg test { + package cfg; + %cfg::thash = (); threads::shared::share(%cfg::thash); } + { lock(%cfg::thash); $tmp1 = {}; $tmp2 = {}; share($tmp1); share($tmp2); + + $v1="B"; $v2="2"; $cfg::thash{$v1} = $tmp1; $cfg::thash{$v1}{$v2} = $tmp2; $cfg::thash{$v1}{$v2}{server} ='b.a.com'; print "$cfg::thash{$v1}{$v2}{server}\n"; + $tmp1 = {}; share($tmp1); $tmp2 = {}; share($tmp2); + $v1='A'; $v2='1'; $cfg::thash{$v1} = $tmp1; $cfg::thash{$v1}{$v2} = $tmp2; $cfg::thash{$v1}{$v2}{server} ='a.a.com'; print "$cfg::thash{$v1}{$v2}{server}\n"; print "$cfg::thash{'B'}{'2'}{server}\n"; + $tmp1 = {}; share($tmp1); $tmp2 = {}; share($tmp2); $v1='C'; $v2='3'; $cfg::thash{$v1} = $tmp1; $cfg::thash{$v1}{$v2} = $tmp2; $cfg::thash{$v1}{$v2}{server} ='c.a.com'; + } for $key1 (sort keys %cfg::thash) { print "ISP:$key1\t"; for $key2 (sort keys %{$cfg::thash{$key1}} ) { print "RANK:$key2\t"; for $key3 (sort keys %{$cfg::thash{$key1}{$key2}}) { print "$key3 $cfg::thash{$key1}{$key2}{$key3}\n"; } } } + + $thr1 = threads->new(\&sub5, "one"); exit; + sub sub5 { my $thr = $_[0]; my %thash=(); my $key ='test'; while (1) { { lock(%cfg::thash); print "Before: thread $thr: thash = $cfg::thash{'A'}{'1'}{ser +ver}\n"; print "After: thread $thr: thash = $cfg::thash{'B'}{'2'}{serv +er}\n"; # $thash{$key} = dclone($cfg::thash{'B'}); $thash{$key} = clone($cfg::thash{'B'}); } } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-24 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found