Subject: | Does it work rightly? |
Hello, thanks to your module.
Thread-Detach-0.01's document says,
1. sets a flag to mark the thread as "detached".
2. marks the thread as "done" if the thread was marked as "detached".
but I couldn't think so when I saw its source code.
l.55 : *threads::detach = sub { $detached{threads->tid} = 1 }
I think 'threads->tid' returns a wrong number (ex. always '0') ?
l.49 : end => sub { $done{threads->tid} = 1 }
It doesn't check 'detached' mark.
My understanding is:
*** Detach.pm.org Thu Feb 12 04:28:26 2004
--- Detach.pm.mod Thu Feb 12 05:20:33 2004
***************
*** 1,5 ****
--- 1,6 ----
package Thread::Detach;
+ use Carp;
# Make sure we have version info for this module
$VERSION = '0.01';
***************
*** 21,27 ****
require threads::shared;
my %detached : shared;
my %done : shared;
!
# Get Thread::Exit
# Set up the necessary Thread::Exit capabilities
# Make sure that all subsequent threads get the same
--- 22,28 ----
require threads::shared;
my %detached : shared;
my %done : shared;
! my $join;
# Get Thread::Exit
# Set up the necessary Thread::Exit capabilities
# Make sure that all subsequent threads get the same
***************
*** 38,58 ****
begin => sub {
lock( %done );
while (my $tid = each %done) {
! threads->object( $tid )->join if delete $done{$tid};
}
},
# End this thread with
# Mark this thread as done
! end => sub { $done{threads->tid} = 1 }
);
# Allow for dirty tricks
# Replace the detach method with one that just sets the detached flag
no warnings 'redefine';
! *threads::detach = sub { $detached{threads->tid} = 1 };
}
}
} #BEGIN
--- 39,83 ----
begin => sub {
lock( %done );
while (my $tid = each %done) {
! if(delete $done{$tid}){
! if( my $th = threads->object( $tid ) ){
! $join->($th);
! }
! }
}
},
# End this thread with
# Mark this thread as done
! end => sub {
! my $tid = threads->tid;
! $done{$tid} = 1 if($detached{$tid});
! }
);
# Allow for dirty tricks
# Replace the detach method with one that just sets the detached flag
no warnings 'redefine';
! *threads::detach = sub {
! lock( %detached );
! $detached{ shift->tid } = 1;
! };
!
! $join = \&threads::join;
!
! *threads::join = sub {
! my $th = shift;
! my $tid = $th->tid;
! if($detached{$tid}){
! croak "Cannot join a detached thread";
! }
! else{
! $join->($th);
! }
! }
!
}
}
} #BEGIN
(test on windows2000 with activeperl 5.8.0&5.8.1)
But do I have misunderstanding about it?