Commit | Line | Data |
d21067e0 |
1 | package Thread::Queue; |
2 | use Thread qw(cond_wait cond_broadcast); |
3 | |
d516a115 |
4 | =head1 NAME |
5 | |
6 | Thread::Queue - thread-safe queues |
7 | |
8 | =head1 SYNOPSIS |
9 | |
10 | use Thread::Queue; |
11 | my $q = new Thread::Queue; |
12 | $q->enqueue("foo", "bar"); |
13 | my $foo = $q->dequeue; # The "bar" is still in the queue. |
14 | |
15 | =cut |
16 | |
d21067e0 |
17 | sub new { |
18 | my $class = shift; |
19 | return bless [@_], $class; |
20 | } |
21 | |
22 | sub dequeue { |
23 | use attrs qw(locked method); |
24 | my $q = shift; |
25 | cond_wait $q until @$q; |
26 | return shift @$q; |
27 | } |
28 | |
29 | sub enqueue { |
30 | use attrs qw(locked method); |
31 | my $q = shift; |
32 | push(@$q, @_) and cond_broadcast $q; |
33 | } |
34 | |
35 | 1; |