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

Lexical Object copied to new thread

Thread Next
From:
Shmuel Fomberg
Date:
December 23, 2007 10:46
Subject:
Lexical Object copied to new thread
Message ID:
000001c84594$0ad3ace0$6801a8c0@semuel7
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";
}

run_test();
---- end code

On ActiveState Perl 5.8.8 build 822, with threads 1.63, I get the following
output:
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?

Thanks,
Shmuel.




Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About