Should we use Thread::Queue::Any instead of Thread::Queue?
I've made a simple script to test them (see below), and the results were:
------------------------------------ TEST QUEUE:51(seconds) ------------------------------------ ------------------------------------ TEST QUEUE ANY:27(seconds) ------------------------------------
I've detected a lot more of LOCKs when running Thread:Queue rather than with Thread::Queue::Any.
#!/opt/coolstack/bin/perl use strict; use threads ('yield', 'stack_size' => 64*4096, 'exit' => 'threads_only', 'stringify'); use Thread::Queue; use Thread::Queue::Any; my $nloaders = 16; my $nworkers = 16; #--------------------------------------------------------------- my $queue = Thread::Queue->new(); my $queue_any = Thread::Queue::Any->new(); my $st = time; #---------------- # TEST QUEUE #---------------- my @thrs_loaders; push @thrs_loaders, threads->create(\&load_hash, 0) for (1..$nloaders) +; my @thrs_workers; push @thrs_workers, threads->create(\&process_hash, 0) for (1..$nworke +rs); $_->join for @thrs_loaders; $queue->enqueue({'STOP' => 1}) for (1..$nworkers); $_->join for @thrs_workers; print "------------------------------------\n"; print "TEST QUEUE:" . (time - $st) . "(seconds)\n"; print "------------------------------------\n"; print "Enter an key to continue...\n"; <STDIN>; $st = time; #---------------- # TEST QUEUE ANY #---------------- @thrs_loaders = (); push @thrs_loaders, threads->create(\&load_hash, 1) for (1..$nloaders) +; @thrs_workers = (); push @thrs_workers, threads->create(\&process_hash, 1) for (1..$nworke +rs); $_->join for @thrs_loaders; my %hash = ('STOP' => 1); # Updated after Browser +Uk Comments $queue_any->enqueue(%hash) for (1..$nworkers); # $_->join for @thrs_workers; print "------------------------------------\n"; print "TEST QUEUE ANY:" . (time - $st) . "(seconds)\n"; print "------------------------------------\n"; exit; #--------------------------------------------------------------------- sub load_hash{ my $test_queue_any = shift || 0; my $id = threads->tid(); my $tmp; print "START LOAD $id\n"; for my $i(1..10000){ my %rec = ('ola' => '1', 'ola1' => '2' ); if($test_queue_any){ $queue_any->enqueue(%rec); }else{ $queue->enqueue(\%rec); } } print "STOP LOAD $id\n"; } #--------------------------------------------------------------------- sub process_hash{ my $test_queue_any = shift || 0; my $id = threads->tid(); my $tmp; print "START WORKER $id\n"; if($test_queue_any){ while(my %hash = $queue_any->dequeue()){ last if exists $hash{'STOP'}; } }else{ while( my $ref = $queue->dequeue() ){ last if exists $ref->{'STOP'}; } } print "STOP WORKER $id\n"; }
In reply to Thread::Queue vs Thread::Queue::Any by gulden
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |