Hi, threads::shared says in its BUG AND LIMITATIONS section:
It is often not wise to share an object unless the class itself has been written to support sharing. For example, an object's destructor may get called multiple times, once for each thread's scope exit.

When trying to test your program, use strict; revealed that several variables are used without initialising them (e.g. precious $cacheRefShar). I have fiddled around with shared_clone() and Cache::MemoryCache but to no avail. Again, studying the documentation lead to Cache::SharedMemoryCache which helped a lot. So this works with Perl 5.8.8 (Linux). Here's a draft...
#!/usr/bin/perl use strict; # so useful use threads; use threads::shared; use Cache::SharedMemoryCache; my $lockCache:shared; #marks critical section my $cache = new Cache::SharedMemoryCache(); sub checkCache ($) { my $key = shift; my $val; { lock $lockCache; $val = $cache->get( $key ); print "checking $key = ($val)\n"; } return $val; } sub setCache ($$) { my($key, $newval) = @_; { print "\tsetting $key=$newval\n"; lock $lockCache; $cache->set( $key, $newval, "1 minutes" ); } } # sanity check - test entry from parent thread $|=1; my $testval = int 1 + rand(1234567); print "TESTing: set($testval)... "; setCache("test", "$testval") or die "set failed"; my $testvalget = checkCache("test") or die "get failed\n"; die "got ill. response ($testvalget) from cache" unless $testvalget eq + $testval; print "TESTing finished OK\n"; # to tell when all threads are complete # keep from blocking in a join, so we can accept signals my $threadTerm :shared =0; my $thr1 = threads->new(\&t,1); my $thr2 = threads->new(\&t,10); my $thr3 = threads->new(\&t,20); sub t { my $id = shift; my $sanitycheck = checkCache("test"); print "====== Starting T$id: 'test' is ($sanitycheck) ====\n"; die "seems, that we cannot share the cache right now..." unless $s +anitycheck; sleep (int rand(3)); foreach my $i (1...10) { print "T$id-$i:\t"; my $key = $i + int (rand 3); my $val = checkCache($key); if (! defined $val ) { $val = $id+rand(10); 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; print "C'est ca...\n";

In reply to Re^3: Threads sharing a Cache::MemoryCache object?? by Perlbotics
in thread 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.