#!/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 undefined value at testCache.pl line 65. $val= $cacheRefShar->get( $key ); #Thread 1 terminated abnormally: Can't call method "get" on unblessed 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 }