Re: sharing objects

[email protected] (Dean Arnold) Tue, 10 Oct 2006 12:38:53 -0700
Newsgroups perl.ithreads
Message-ID <[email protected]>
Jack Steadman wrote:
> On 10/10/06, Dean Arnold <[email protected]> wrote:
>> Dean Arnold wrote:
>> Big OOOPS on my part. I forgot one little detail: you have to rebless
>> the object when its dequeue'd:
> 
>> #
>> #       You have to rebless in the receiving thread
>> #
>>         bless $pktobj, 'Packet';
> 
> I've found it convenient to store the class name as a data member of
> the class and implement a simple 'rebless' function to automatically
> rebless shared objects when necessary.  So for example:
> 
> $obj = rebless($queue->dequeue());
> 
> sub rebless {
>   my $obj = shift;
>   return bless $obj, $obj->{'class_name'};
> }
> 
> You could even subclass or wrap Thread::Queue to do this automatically
> if you know that all of your queued items will be objects which follow
> this pattern.
> 
> Jack
> 

Thats the general idea behind Thread::Queue::Duplex's Thread::Queue::Queueable
base class, except its more generic to permit app-specific marshalling.
I've considered making it a default behavior in TQQ, but since its hard
to know what the members of an object might be - which might require deep
traversal, or even Storable to handle - I've kept the default curse()/redeem()
methods fairly simple. Thread::Apartment implements a more complex/complete version
to marshall params back&forth between apartment threaded objects (including
support for passing closure proxies).

- Dean