develooper Front page | perl.ithreads | Postings from December 2007

RE: Lexical Object copied to new thread

Thread Previous | Thread Next
Shmuel Fomberg
December 23, 2007 12:30
RE: Lexical Object copied to new thread
Message ID:
Hi Liz.

Nice module. Can save me saving the creator thread id in the object.

Anyway, my question is that $x is a lexical inside run_test, and as much as
I understood it was not suppose to be copied to the new thread. But it is.

Actually, I come to this question from elsewhere. I developed an embedded
Perl inside a C program. The idea was that I (the C side) call new on some
module, get an object, and makes all the calls on methods of this object.
Now the Perl turned to be multi-threaded Perl. And I found to my surprise
that when that object is creating child-threads, the object (that only the C
side have reference to) is being copied too.

However, in the beginning of every method, including the one that creates
the thread, there is "my $self = shift". So now the object is held by a
lexical inside the method. And I thought that lexical are not copied. Maybe
I am wrong. Maybe it's a bug. I don't know what to think anymore.


>-----Original Message-----
>From: Elizabeth Mattijsen []
>Sent: Sunday, December 23, 2007 9:02 PM
>To: Shmuel Fomberg;
>Subject: Re: Lexical Object copied to new thread
>At 8:58 PM +0200 12/23/07, Shmuel Fomberg wrote:
>>Hello List.
>>Please observe the following code:
>>---- start code
>>#!/usr/bin/perl -w
>>use strict;
>>use threads;
>>sub thr {
>>     print "Thread running.. Done.\n";
>>sub run_test {
>>     my $x = bless {Name=>"Moshe"}, 'Obj';
>>     threads->create(\&thr)->join();
>>sub Obj::DESTROY {
>>     my $self = shift;
>>     print "Object ", $self, " whose name is ", $self->{Name};
>>     print " Destroyed from thread ", threads->tid, "\n";
>>---- end code
>>On ActiveState Perl 5.8.8 build 822, with threads 1.63, I get the
>>Thread running.. Done.
>>Object Obj=HASH(0x18c42f0) whose name is Moshe Destroyed from thread 1
>>Object Obj=HASH(0x226f70) whose name is Moshe Destroyed from thread 0
>>What I conclude from this is that the object is being copied to the new
>>thread, along with its data, and is destroyed when the thread ends.
>>Is it bug or feature?
>You might want to check my Thread::Bless module on CPAN in this respect.
>No virus found in this incoming message.
>Checked by AVG Free Edition.
>Version: 7.5.516 / Virus Database: 269.17.6/1193 - Release Date: 22/12/2007

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About