This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix makefile to put queue.pm correctly, update test script.
[perl5.git] / ext / threads / shared / t / queue.t
CommitLineData
6d1f61ba
AB
1
2
3BEGIN {
89661126
AB
4 chdir 't' if -d 't';
5 push @INC ,'../lib';
6 require Config; import Config;
7 unless ($Config{'useithreads'}) {
6d1f61ba
AB
8 print "1..0 # Skip: might still hang\n";
9 exit 0;
89661126 10 }
6d1f61ba
AB
11}
12
13
14use threads;
0db258a5 15use threads::shared::queue;
6d1f61ba
AB
16
17$q = new threads::shared::queue;
cd055192 18$|++;
89661126
AB
19print "1..26\n";
20
2a4145a8 21my $test : shared = 1;
974236b4 22
6d1f61ba
AB
23sub reader {
24 my $tid = threads->self->tid;
25 my $i = 0;
26 while (1) {
27 $i++;
28# print "reader (tid $tid): waiting for element $i...\n";
29 my $el = $q->dequeue;
974236b4 30 print "ok $test\n"; $test++;
6d1f61ba 31# print "reader (tid $tid): dequeued element $i: value $el\n";
89661126 32 select(undef, undef, undef, rand(1));
6d1f61ba
AB
33 if ($el == -1) {
34 # end marker
35# print "reader (tid $tid) returning\n";
36 return;
37 }
38 }
39}
40
89661126 41my $nthreads = 5;
6d1f61ba
AB
42my @threads;
43
44for (my $i = 0; $i < $nthreads; $i++) {
45 push @threads, threads->new(\&reader, $i);
46}
47
89661126 48for (my $i = 1; $i <= 20; $i++) {
6d1f61ba 49 my $el = int(rand(100));
89661126 50 select(undef, undef, undef, rand(1));
6d1f61ba
AB
51# print "writer: enqueuing value $el\n";
52 $q->enqueue($el);
53}
54
55$q->enqueue((-1) x $nthreads); # one end marker for each thread
56
57for(@threads) {
89661126 58# print "waiting for join\n";
6d1f61ba 59 $_->join();
6d1f61ba 60}
974236b4 61print "ok $test\n";
6d1f61ba
AB
62
63