Once again. Multiple threads, a root thread, and multiple processing threads (looking at emails). I am trying to define a shared Cache::MemoryCache object that holds Message-Ids for all the threads.

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 }

In reply to Threads sharing a Cache::MemoryCache object?? by Wiggins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.