Wiggins has asked for the wisdom of the Perl Monks concerning the following question:
Running the supplied prototype code, shows that when it compiles and runs; each thread has a thread-local copy of the cache. I see each thread testing if '2' exists and then setting it's own value.
I have tried 'use vars', 'our', sharing a reference to the object; generally resulting in syntax errors, blessing errors, or local objects.
I have read 'man threads::shared", but won't claim to understand
"Due to problems with Perl’s prototyping, if you want to share a newly created reference, you need to use the "&share([])" and "&share({})" syntax."
#!/usr/bin/perl use threads; use threads::shared; use Cache::MemoryCache; # the history cache my $lockCache:shared; #marks critical section ## Shotgun grammar follows ... hoping something works. #use vars qw($cacheRef); # use vars qw($cache); $cache = new Cache::MemoryCache( ); #our $cache = new Cache::MemoryCache( ); #my $cacheRef =\$cache; #my $cacheRefShar = share($cacheRef); # to tell when all threads are complete # keep from blocking in a join, so we can accept signals my $threadTerm :shared =0; sub checkCache ($); sub setCache ($$); $thr1 = threads->new(\&t,1); $thr2 = threads->new(\&t,10); $thr3 = threads->new(\&t,20); #t (1); sub t { my $id=shift; sleep (int rand(3)); foreach $i (1...10) { print "T$id-$i\n"; my $key = $i + int (rand 3); my $val = checkCache($key); #print "check returned ($val)\n"; if (! defined $val ) { $val = $id+rand(10); #print "T%id-$i $key=$val\n"; setCache ($key, $val); } sleep (int rand (3)); } $threadTerm++; } # to allow future signals to be recognized while ($threadTerm <3) { sleep 2; } $thr1->join; $thr2->join; $thr3->join; ### fini #### sub checkCache ($) { my $key = shift; my $val; { lock $lockCache; #$val= $$cacheRefShar->get( $key ); #Thread 1 terminated abnormally: Can't call method "get" on an u +ndefined value at testCache.pl line 65. $val= $cacheRefShar->get( $key ); #Thread 1 terminated abnormally: Can't call method "get" on unbl +essed reference at testCache.pl line 65. $val= $cache->get( $key ); print "checking $key = ($val)\n"; } return $val; } sub setCache ($$) { my($key, $newval) = @_; { printf "setting $key=$newval\n"; lock $lockCache; $$cacheRefShar->set( $key, $newval, "3 minutes" ); } } sub testAndSet ($$) { #not yet }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Threads sharing a Cache::MemoryCache object??
by perrin (Chancellor) on Aug 08, 2008 at 19:30 UTC | |
by Wiggins (Hermit) on Aug 09, 2008 at 17:02 UTC | |
by Perlbotics (Archbishop) on Aug 10, 2008 at 11:24 UTC |