trouble assigning references to shared objects

[email protected] ("Jack Steadman")
Newsgroups perl.ithreads
Message-ID <[email protected]>
I am having some trouble using shared objects, even when they're not being
accessed by more than one thread.  My problem will be better illustrated
through code snippets than by me trying to explain it, so... without going
into any details, i have an object which is itself a hash ref and has a
couple of hash refs as data members:

package Conn;

sub new {
	my $class = shift;

	...

	my $conn = &share({});
	$conn->{tx} = &share({});
	$conn->{rx} = &share({});

	...

	return bless $conn, $class;
}

Later, when i try to assign shared objects of other classes to one of the
tx/rx hash refs, i have problems with those objects seeming to lose their
blessing:

sub addToTx {
	my $self = shift;
	my $txid = shift;
	my $objectref = shift;	## assume $objectref is shared

	print ref($objectref);

	lock($self);
	$self->{tx}->{$txid} = $objectref;

	print ref($self->{tx}->{$txid});
}

The first ref() in this case prints the class of the object, say
"MyMail::Subject," while the second ref() prints just "HASH".  The hash that
is referenced by $self->{tx}->{$txid} appears to contain all of the data
that the object held, but I can't call methods because the object has
reverted to a hash reference.

This appears to happen only in a threaded environment.  If I extract the
code that's doing this and run it without using threads, the object keeps
its blessing.  Again, assume that object is only being accessed at this
point by one thread (though it will need to be shared between them for other
reasons).  Is this because of quirkiness in the way shared references are
handled?  Is there a good way around this problem?

Thanks,
Jack Steadman
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.