Is there an easy way to trap the /final/ destruction of a shared object?  Under
5.8.1, DESTROY methods may be invoked repeatedly, whenever the blessed ref
goes out of scope in any thread (including thread cleanup of visible lexicals).

For example, the following will DESTROY a single shared object five times:

   use threads;
   use threads::shared;
   use strict;

   package A;

   sub new {
     my $o : shared = 1;
     bless \$o, shift;
   }

   sub DESTROY {
     my $o = shift;
     $$o++;
     print "[thr ", threads->tid, "] $o ($$o)\n";
   }

   package main;
   my $o = A->new;      # Assume $o's constructor share()s the obj.

   async {1;}->join;    # 1) DESTROYed upon thread collection (join())

   async {1;}->detach;  # 2) when internally collected, asynchronous

   $thr = async {$o};

   undef $o;            # 3) run-of-the-mill DESTROY

   my $o2 = $thr->join; # 4) as #1 above

   undef($o2);          # 5) object, returned from $thr, DESTROYed

This produces:

   $ perl dtor.pl
   [thr 1] A=SCALAR(0x8137470) (2)
   [thr 2] A=SCALAR(0x8121570) (3)
   [thr 0] A=SCALAR(0x8061db0) (4)
   [thr 3] A=SCALAR(0x810fa30) (5)
   [thr 0] A=SCALAR(0x81047e4) (6)

I noticed this after looking into segfaults with an XS module under threads,
due, I think, to repeated invocation of its DESTROY/deallocation routine.  Any
suggestions on how I might determine which DESTROY is the very last one?

Regards,
Mike
-- 
Michael J. Pomraning
[EMAIL PROTECTED]
http://pilcrow.madison.wi.us

Reply via email to