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