Pearl: How to make a manufacturer?

I have a function that works very slowly. I need to enter this function in the main part of the program. So I would like to do something similar to the UNIX yes command, which produces as much input as it reads, but only a little more than necessary. Unlike yes I don't need the values ​​from STDIN, but I want the values ​​in the Perl queue.

In other words: this problem does not lie in the selection of file descriptors, but in the queues supported by the threads.

I assume that the meta code will look something like this:

 my $DataQueue = Thread::Queue->new(); my @producers; my $no_of_threads = 10; for (1..$no_of_threads) { push @producers, threads->create(\&producer); } for(<>) { # This should block until there is a value to dequeue # Maybe dequeue blocks by default - then this part is not a problem my $val = $DataQueue->dequeue(); do_something($_,$val); } # We are done: The producers are no longer needed kill @producers; sub producer { while(1) { # How do I wait until the queue length is smaller than number of threads? wait_until(length of $DataQueue < $no_of_threads); $DataQueue->enqueue(compute_slow_value()); } } 

But is there a more elegant way to do this? I especially don't know how to effectively do wait_until .

+6
source share
1 answer

Maybe something like this works:

 my $DataQueue = Thread::Queue->new(); my @producers; my $no_of_threads = 10; for (1..$no_of_threads) { push @producers, threads->create(\&producer); } $DataQueue->limit = 2 * $no_of_threads; for(<>) { # This blocks until $DataQueue->pending > 0 my $val = $DataQueue->dequeue(); do_something($_,$val); } # We are done: The producers are no longer needed kill @producers; sub producer { while(1) { # enqueue will block until $DataQueue->pending < $DataQueue->limit $DataQueue->enqueue(compute_slow_value()); } } 
0
source

All Articles