This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Thread::Queue 3.06
[perl5.git] / dist / Thread-Queue / t / 07_lock.t
CommitLineData
54c7876f
JH
1use strict;
2use warnings;
3
4BEGIN {
54c7876f
JH
5 use Config;
6 if (! $Config{'useithreads'}) {
3d4f2f89 7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
54c7876f
JH
8 exit(0);
9 }
10}
11
12use threads;
13use Thread::Queue;
14use Thread::Semaphore;
15
16if ($] == 5.008) {
17 require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
18} else {
19 require Test::More;
20}
21Test::More->import();
22plan('tests' => 3);
23
24# The following tests locking a queue
25
26my $q = Thread::Queue->new(1..10);
27ok($q, 'New queue');
28
29my $sm = Thread::Semaphore->new(0);
30my $st = Thread::Semaphore->new(0);
31
e128eaa1 32threads->create(sub {
54c7876f
JH
33 {
34 lock($q);
35 $sm->up();
36 $st->down();
37 threads::yield();
38 select(undef, undef, undef, 0.1);
39 my @x = $q->extract(5,2);
40 is_deeply(\@x, [6,7], 'Thread dequeues under lock');
41 }
e128eaa1 42})->detach();
54c7876f
JH
43
44$sm->down();
45$st->up();
46my @x = $q->dequeue_nb(100);
47is_deeply(\@x, [1..5,8..10], 'Main dequeues');
48threads::yield();
49
3d4f2f89
JH
50exit(0);
51
54c7876f 52# EOF