Commit | Line | Data |
---|---|---|
13c1b207 | 1 | use warnings; |
6d1f61ba | 2 | |
3 | BEGIN { | |
89661126 | 4 | chdir 't' if -d 't'; |
5 | push @INC ,'../lib'; | |
6 | require Config; import Config; | |
7 | unless ($Config{'useithreads'}) { | |
83272a45 | 8 | print "1..0 # Skip: no ithreads\n"; |
6d1f61ba | 9 | exit 0; |
89661126 | 10 | } |
6d1f61ba | 11 | } |
12 | ||
13c1b207 | 13 | use strict; |
6d1f61ba | 14 | use threads; |
83272a45 | 15 | use Thread::Queue; |
6d1f61ba | 16 | |
83272a45 | 17 | my $q = new Thread::Queue; |
cd055192 | 18 | $|++; |
89661126 | 19 | print "1..26\n"; |
20 | ||
2a4145a8 | 21 | my $test : shared = 1; |
974236b4 | 22 | |
a438a718 | 23 | sub ok { |
24 | lock($test); | |
25 | print "ok $test\n"; | |
26 | $test++; | |
27 | } | |
28 | ||
6d1f61ba | 29 | sub reader { |
30 | my $tid = threads->self->tid; | |
31 | my $i = 0; | |
32 | while (1) { | |
33 | $i++; | |
34 | # print "reader (tid $tid): waiting for element $i...\n"; | |
35 | my $el = $q->dequeue; | |
a438a718 | 36 | ok(); |
37 | # print "ok $test\n"; $test++; | |
6d1f61ba | 38 | # print "reader (tid $tid): dequeued element $i: value $el\n"; |
89661126 | 39 | select(undef, undef, undef, rand(1)); |
6d1f61ba | 40 | if ($el == -1) { |
41 | # end marker | |
42 | # print "reader (tid $tid) returning\n"; | |
43 | return; | |
44 | } | |
45 | } | |
46 | } | |
47 | ||
89661126 | 48 | my $nthreads = 5; |
6d1f61ba | 49 | my @threads; |
50 | ||
51 | for (my $i = 0; $i < $nthreads; $i++) { | |
52 | push @threads, threads->new(\&reader, $i); | |
53 | } | |
54 | ||
89661126 | 55 | for (my $i = 1; $i <= 20; $i++) { |
6d1f61ba | 56 | my $el = int(rand(100)); |
89661126 | 57 | select(undef, undef, undef, rand(1)); |
6d1f61ba | 58 | # print "writer: enqueuing value $el\n"; |
59 | $q->enqueue($el); | |
60 | } | |
61 | ||
62 | $q->enqueue((-1) x $nthreads); # one end marker for each thread | |
63 | ||
64 | for(@threads) { | |
89661126 | 65 | # print "waiting for join\n"; |
6d1f61ba | 66 | $_->join(); |
6d1f61ba | 67 | } |
a438a718 | 68 | ok(); |
69 | #print "ok $test\n"; | |
6d1f61ba | 70 | |
71 |