Added queue.pm and test case, still disabled because of discovered race (or am I...
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / t / queue.t
1
2
3 BEGIN {
4 #    chdir 't' if -d 't';
5 #    push @INC ,'../lib';
6 #    require Config; import Config;
7 #    unless ($Config{'useithreads'}) {
8         print "1..0 # Skip: might still hang\n";
9         exit 0;
10 #    }
11 }
12
13
14 use threads;
15 use threads::queue;
16
17 $q = new threads::shared::queue;
18
19 sub reader {
20     my $tid = threads->self->tid;
21     my $i = 0;
22     while (1) {
23         $i++;
24 #       print "reader (tid $tid): waiting for element $i...\n";
25         my $el = $q->dequeue;
26 #       print "reader (tid $tid): dequeued element $i: value $el\n";
27         select(undef, undef, undef, rand(2));
28         if ($el == -1) {
29             # end marker
30 #           print "reader (tid $tid) returning\n";
31             return;
32         }
33     }
34 }
35
36 my $nthreads = 1;
37 my @threads;
38
39 for (my $i = 0; $i < $nthreads; $i++) {
40     push @threads, threads->new(\&reader, $i);
41 }
42
43 for (my $i = 1; $i <= 10; $i++) {
44     my $el = int(rand(100));
45     select(undef, undef, undef, rand(2));
46 #    print "writer: enqueuing value $el\n";
47     $q->enqueue($el);
48 }
49
50 $q->enqueue((-1) x $nthreads); # one end marker for each thread
51
52 for(@threads) {
53         print "waiting for join\n";
54         $_->join();
55         
56 }
57
58
59