Guest User

Untitled

a guest
May 10th, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package Foo;
  2. use strict;
  3. use warnings;
  4. use threads;
  5. use threads::shared;
  6. use Thread::Queue;
  7. {
  8.     use Object::InsideOut qw(:SHARED); 
  9.    
  10.     my @foo :Field :Default([]);
  11.     my @bar :Field :Default({});
  12.     my @baz :Field :Default('');
  13.     my @bat :Field :Default([]);
  14.     my @a :Field :Default([]);
  15.     my @b :Field :Default({});
  16.     my @c :Field :Default('');
  17.     my @d :Field :Default('');
  18.    
  19.     sub _destroy :Destroy {
  20.         my ($self) = @_;
  21.        
  22.         my $tid = threads->tid;
  23.        
  24.         print "Destroy of $$self not in consumer thread (in $tid)\n" unless $tid == 3;
  25.     }
  26. }
  27.  
  28. package main;
  29. use threads;
  30. use threads::shared;
  31. use Thread::Queue;
  32.  
  33. my $q = Thread::Queue->new();
  34.  
  35. # creators
  36. threads->create(sub { while (1) { sleep(rand(1)); $q->enqueue(Foo->new); }  })->detach();
  37. threads->create(sub { while (1) { sleep(rand(2)); $q->enqueue(Foo->new); }  })->detach();
  38. # consumer
  39. threads->create(sub { while (my $t = $q->dequeue) {} })->join();
Advertisement
Add Comment
Please, Sign In to add comment