"Clean" implementation of binmode(FH)/":raw" identity.
[p5sagit/p5-mst-13.2.git] / ext / Thread / queue.tx
CommitLineData
0fcb073c 1BEGIN {
2 eval { require Config; import Config };
3 if ($@) {
4 print "1..0 # Skip: no Config\n";
5 exit(0);
6 }
0fcb073c 7}
8
19be36ba 9use Thread;
10use Thread::Queue;
11
12$q = new Thread::Queue;
13
14sub reader {
50112d62 15 my $tid = Thread->self->tid;
16 my $i = 0;
17 while (1) {
18 $i++;
19 print "reader (tid $tid): waiting for element $i...\n";
19be36ba 20 my $el = $q->dequeue;
50112d62 21 print "reader (tid $tid): dequeued element $i: value $el\n";
22 select(undef, undef, undef, rand(2));
23 if ($el == -1) {
24 # end marker
25 print "reader (tid $tid) returning\n";
26 return;
27 }
19be36ba 28 }
29}
30
50112d62 31my $nthreads = 3;
32
33for (my $i = 0; $i < $nthreads; $i++) {
34 Thread->new(\&reader, $i);
35}
36
37for (my $i = 1; $i <= 10; $i++) {
19be36ba 38 my $el = int(rand(100));
39 select(undef, undef, undef, rand(2));
40 print "writer: enqueuing value $el\n";
41 $q->enqueue($el);
42}
50112d62 43
44$q->enqueue((-1) x $nthreads); # one end marker for each thread