01-06-05 22.16, skrev Jenda Krynicky p� [EMAIL PROTECTED] f�ljande:
>
>> OTOH people are asking this for fork() aswell. We were discussing this on
>> #perl the other day. A general solution where you could register for objects
>> to become null on fork() would be nice.
>
> I think it would be better if you could mark the whole class as "null
> on fork()" but if this would be too hard to implement even per
> instance would be helpfull.
We must guess alot to do that!
> Though with modules like DBI it might not help.
>
> Actualy how does perl clone the data? Does it traverse all references
> copying what it sees like a copying garbage collector ?
Yes.
>> Using weakrefs I think this is doable. You hand it the ref it should void.
>> And then after the fork you tell it to empty those objects. (This needs XS)
>
> I see. As a test I tried to write something like this using the bless
> trick. I attach it ... hope nobody minds.
>
>> I think it would be nice for a module to be able to declare itself
>> threadsafe. Not realy sure what the best approach is!
>>
>> Artur
>
> Yes that yould be good.
>
> Jenda
>
> == [EMAIL PROTECTED] == http://Jenda.Krynicky.cz ==
> : What do people think?
> What, do people think? :-)
> -- Larry Wall in <[EMAIL PROTECTED]>
>
>
> package IThread::Jenda; # don't know a good name
>
> use Exporter;
> use WeakRef;
>
> @ISA = qw(Exporter);
> @EXPORT = qw(null_on_fork Fork);
>
> my @objects;
>
> sub null_on_fork {
> my $obj = shift;
> my $ref= $obj;
> weaken $ref;
this won't work, weaken weakens only the ref it currently exists on, you
need to push @objects, $ref;
weak($objects[-1]);
> push @objects, $ref;
> 1;
> }
>
> sub Fork {
> my $pid = fork();
> return unless defined $pid; # fork() failed
> return $pid if $pid; # we are in parent
> my $ref;
> foreach $ref (@objects) {
> next unless defined $ref;
> bless $ref, 'rotfl_this_if_insane';
> undef $ref;
> }
> undef @objects;
> return 0;
> }
>
> 1;
This might work, try it?
Artur