Guest User

Object::InsideOut thread/queue problem

a guest
Mar 3rd, 2010
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.35 KB | None | 0 0
  1. use threads;
  2. use threads::shared;
  3. use Thread::Queue;
  4. use feature 'say';
  5. require RegularClass;
  6. require InsideOutClass;
  7.  
  8.  
  9. my $q = new Thread::Queue;
  10.  
  11. my $thread = threads->create(\&th_sub1)->join;
  12.  
  13. while (my $o = $q->dequeue_nb)
  14. {
  15.     say $o->id;
  16.     # $o going out of scope: destroy message shows
  17. };
  18.  
  19.  
  20. $thread = threads->create(\&th_sub)->join;
  21.  
  22. while (my $o = $q->dequeue_nb)
  23. {
  24.     say $o->id;
  25.     # $o going out of scope: no destroy message! memory is still used!
  26.     # furthermore: use Devel::Refcount::refcount($o) shows 3 (??)
  27.    
  28. };
  29.  
  30.  
  31. sub th_sub
  32. {
  33.     for (1..100)
  34.     {
  35.         my $o = new InsideOutClass;
  36.         $q->enqueue($o);
  37.     }
  38. }
  39.  
  40. sub th_sub1
  41. {
  42.     for (1..100)
  43.     {
  44.         my $o = new RegularClass;
  45.         $q->enqueue($o);
  46.     }
  47. }
  48.  
  49. # ----------------------------
  50.  
  51. package RegularClass;
  52.  
  53. sub new
  54. {
  55.     my $class = shift;
  56.  
  57.     my $self = shared_clone({});
  58.     $self->{id} = int(rand(1000));
  59.    
  60.     return bless $self, $class;
  61. }
  62.  
  63. sub DESTROY
  64. {
  65.     my $self = shift;
  66.    
  67.     say "Destroing object with id = ". $self->{id};
  68. }
  69.  
  70. 1;
  71.  
  72. # ----------------------------
  73.  
  74. package InsideOutClass;
  75.  
  76. {
  77.     use Object::InsideOut qw(:SHARED);
  78.    
  79.     my @id :Field :Acc(id);
  80.    
  81.     sub init :Init
  82.     {
  83.         my $self = shift;
  84.    
  85.         $self->set(\@id, int(rand(1000))); 
  86.     }
  87.    
  88.     sub destroy :Destroy
  89.     {
  90.         my $self = shift;
  91.        
  92.         say "Destroing object with id = ". $self->{id};        
  93.     }
  94. }
  95.  
  96. 1;
Advertisement
Add Comment
Please, Sign In to add comment