in reply to Multi-thread combining the results together
Something like this:
use strict; use warnings; use threads; use Thread::Queue; my $workQueue = Thread::Queue->new(); my $doneQueue = Thread::Queue->new(); my @threads; push @threads, threads->create(sub{DoWork($workQueue, $doneQueue)}) fo +r 1 .. 4; for (1 .. 10000) { my $work = int(rand(10000)); $workQueue->enqueue($work); } $workQueue->end(); $_->join() for @threads; print "$_\n" while $doneQueue->pending() && ($_ = $doneQueue->dequeue( +)); exit; sub DoWork { my ($work, $done) = @_; while (my $item = $work->dequeue()) { $done->enqueue($item) if $item >= 10 && $item <= 20; } }
Prints (YMMV):
17 14 11 19 12 10 13 10 11
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Multi-thread combining the results together
by marioroy (Prior) on Jul 25, 2019 at 11:58 UTC | |
|
Re^2: Multi-thread combining the results together
by Marshall (Canon) on Jul 31, 2019 at 03:48 UTC | |
|
Re^2: Multi-thread combining the results together
by Marshall (Canon) on Aug 02, 2019 at 05:10 UTC | |
|
Re^2: Multi-thread combining the results together
by Anonymous Monk on Jul 25, 2019 at 07:46 UTC | |
by roboticus (Chancellor) on Jul 25, 2019 at 13:55 UTC | |
by marioroy (Prior) on Jul 26, 2019 at 01:15 UTC | |
|
Re^2: Multi-thread combining the results together
by Anonymous Monk on Aug 02, 2019 at 07:43 UTC |