Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use threads;
- use threads::shared;
- use Thread::Queue;
- use feature 'say';
- require RegularClass;
- require InsideOutClass;
- my $q = new Thread::Queue;
- my $thread = threads->create(\&th_sub1)->join;
- while (my $o = $q->dequeue_nb)
- {
- say $o->id;
- # $o going out of scope: destroy message shows
- };
- $thread = threads->create(\&th_sub)->join;
- while (my $o = $q->dequeue_nb)
- {
- say $o->id;
- # $o going out of scope: no destroy message! memory is still used!
- # furthermore: use Devel::Refcount::refcount($o) shows 3 (??)
- };
- sub th_sub
- {
- for (1..100)
- {
- my $o = new InsideOutClass;
- $q->enqueue($o);
- }
- }
- sub th_sub1
- {
- for (1..100)
- {
- my $o = new RegularClass;
- $q->enqueue($o);
- }
- }
- # ----------------------------
- package RegularClass;
- sub new
- {
- my $class = shift;
- my $self = shared_clone({});
- $self->{id} = int(rand(1000));
- return bless $self, $class;
- }
- sub DESTROY
- {
- my $self = shift;
- say "Destroing object with id = ". $self->{id};
- }
- 1;
- # ----------------------------
- package InsideOutClass;
- {
- use Object::InsideOut qw(:SHARED);
- my @id :Field :Acc(id);
- sub init :Init
- {
- my $self = shift;
- $self->set(\@id, int(rand(1000)));
- }
- sub destroy :Destroy
- {
- my $self = shift;
- say "Destroing object with id = ". $self->{id};
- }
- }
- 1;
Advertisement
Add Comment
Please, Sign In to add comment