Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Foo;
- use strict;
- use warnings;
- use threads;
- use threads::shared;
- use Thread::Queue;
- {
- use Object::InsideOut qw(:SHARED);
- my @foo :Field :Default([]);
- my @bar :Field :Default({});
- my @baz :Field :Default('');
- my @bat :Field :Default([]);
- my @a :Field :Default([]);
- my @b :Field :Default({});
- my @c :Field :Default('');
- my @d :Field :Default('');
- sub _destroy :Destroy {
- my ($self) = @_;
- my $tid = threads->tid;
- print "Destroy of $$self not in consumer thread (in $tid)\n" unless $tid == 3;
- }
- }
- package main;
- use threads;
- use threads::shared;
- use Thread::Queue;
- my $q = Thread::Queue->new();
- # creators
- threads->create(sub { while (1) { sleep(rand(1)); $q->enqueue(Foo->new); } })->detach();
- threads->create(sub { while (1) { sleep(rand(2)); $q->enqueue(Foo->new); } })->detach();
- # consumer
- threads->create(sub { while (my $t = $q->dequeue) {} })->join();
Advertisement
Add Comment
Please, Sign In to add comment